-1

I have a project, in which there is a setter method as shown below:

public Long getvoiceId() {
    return voiceId;
}

Now, I am using DOM parser to generate the xml from the object itself, but for this particular field, I am getting compilation error as shown below.
It always give me the compilation error, indicating that the method's return type should be changed to string.

Element voiceId = doc.createElement("voiceId");
InvoiceId.appendChild(doc.createTextNode(Long.toString(irm.getInvoiceId())));
voiceReferenceNotificationMessage.appendChild(voiceId);

As you can see above, I am explicitly casting it using Long.toString, but I am still getting NullPointerException for this.

Please advise on how to proceed, thanks.

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
ndsfd ddsfd
  • 235
  • 1
  • 5
  • 13

1 Answers1

4

Your getter returns a Long (an object) instead of a long (primitive). An object may be null, while a (initialized) primitive alwaws has to have a value. Long.toString() however takes a long, which means the VM needs to unbox the Long by automatically inserting a .longValue() call on it. If the object is null, you get a NullPointerException

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
loonytune
  • 1,775
  • 11
  • 22