Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BootNotificationRequest::setChargeBoxSerialNumber(null) throws NullPointerException #221

Closed
bantu opened this issue Nov 9, 2022 · 7 comments · Fixed by #223 or #306
Closed

BootNotificationRequest::setChargeBoxSerialNumber(null) throws NullPointerException #221

bantu opened this issue Nov 9, 2022 · 7 comments · Fixed by #223 or #306

Comments

@bantu
Copy link
Contributor

bantu commented Nov 9, 2022

I am not sure whether this is the expected behavior, I do not believe it is.

new BootNotificationRequest().setChargeBoxSerialNumber(null);

throws a NullPointerException due to .length() in

    if (!ModelUtil.validate(chargeBoxSerialNumber, STRING_25_CHAR_MAX_LENGTH)) {
      throw new PropertyConstraintException(
          chargeBoxSerialNumber.length(), validationErrorMessage(STRING_25_CHAR_MAX_LENGTH));
    }

being called on a null String.

Note that chargeBoxSerialNumber is optional in BootNotificationRequest.

Here's a test for reproduction:

package eu.chargetime.ocpp.model.core.test;

import eu.chargetime.ocpp.model.core.BootNotificationRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class BootNotificationRequestTest {

  @Rule public ExpectedException thrownException = ExpectedException.none();

  private BootNotificationRequest request;

  @Before
  public void setup() {
    request = new BootNotificationRequest();
  }

  @Test()
  public void validate_setChargeBoxSerialNumber_onNull_doesNotThrowNPE() {
    request.setChargeBoxSerialNumber(null);
  }
}
bantu added a commit to bantu/Java-OCA-OCPP that referenced this issue Nov 9, 2022
@bantu
Copy link
Contributor Author

bantu commented Nov 9, 2022

@TVolden
Copy link
Member

TVolden commented Nov 11, 2022

Hi @bantu,

Your right, that makes no sense. Thanks for pointing it out.
Maybe the ModelUtil should have a small method to extract lengths or 0 from a string?

If you make a PR, I'll merge it ;)

- Thomas

@jmluy
Copy link
Contributor

jmluy commented Nov 15, 2022

This also affects the other optional fields in that class like chargePointSerial;Number, firmwareVersion, iccid, imsi, meterSerialNumber and meterType.

@bantu
Copy link
Contributor Author

bantu commented Nov 15, 2022

This also affects the other optional fields in that class like chargePointSerial;Number, firmwareVersion, iccid, imsi, meterSerialNumber and meterType.

Yes. Thank you. I was about to mention that. Does it also affect other classes?

@jmluy
Copy link
Contributor

jmluy commented Nov 16, 2022

This also affects the other optional fields in that class like chargePointSerial;Number, firmwareVersion, iccid, imsi, meterSerialNumber and meterType.

Yes. Thank you. I was about to mention that. Does it also affect other classes?

I didn't check all but for some classes they do a check with something like the following.
if (field != null && ...)

I saw some code which probably have the same issue

@bantu
Copy link
Contributor Author

bantu commented Nov 24, 2022

@jmluy I'll focus on BootNotificationRequest.

@bantu
Copy link
Contributor Author

bantu commented Nov 24, 2022

See #223

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants