• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does this throw a nullpointerexception  RSS feed

 
J Slater
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the below code in a comparator and it throws an NPE at the return line when it does the comparison. The idea in the preceding lines was to put a blank character string in the name parameter if it's null. But it throws an NPE.

If instead a setName(" ") with a space instead of double quotes, it does not NPE.

Can anyone share why this is? This same logic worked fine elsewhere in my code when it's just Object->getString(). In this case it's Object->getObject->getString() and does not work.

Thanks!
Jim


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you are setting a new Role object on c1 and c2 twice. In the second pair of if statements, they have a Role object already so just get it and set the name on it.

BTW What does the Role classes setName() method do when an empty string is passed in?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atomz Peace wrote:If instead a setName(" ") with a space instead of double quotes, it does not NPE.

If it accepts a single space as a name but not an empty string, then there is probably some code in the setName method that ignores empty strings. That would be the first thing I'd check.
 
J Slater
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getname and setname are just standard getter/setter. Name is a java String.

public void setName(String name) {
this.name = name;
}

Does that ignore an empty string behind the scenes?

The only reason we created a new object instead of setting the name on the existing object was just for testing. When I debug in eclipse right when it throws the NPE, both name values show a value, but are the empty string. They were not null in eclipse.

That's what is so strange. All values have a value in eclipse, yet it throws an NPE.

Any other ideas why? We can use a space to work-around this, but really curious why a NPE comes.

Thank you all for your help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 suggesttions:

1. Print out each piece of that long return line


2. Check for potential auto-unboxing problems.




 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atomz Peace wrote:The getname and setname are just standard getter/setter. Name is a java String.

public void setName(String name) {
this.name = name;
}

Does that ignore an empty string behind the scenes?


No. Whatever value is in name will get copied to this.name. If that value is a reference, then both reference variables end up pointing to the same object. If that value is null, both reference variables will contain null.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!