• Post Reply Bookmark Topic Watch Topic
  • New Topic

Function returning null crashes code when assigned to variable  RSS feed

 
Mike Cheung
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm stuck with the following bit of code where the function myFunction() can occasionally return a null, and this is not something I can control. The code throws the exception shown whenever this happens. How can I solve this?


Exception thrown ...
Exception in thread "main" java.lang.NullPointerException
 
Tim Cooke
Marshal
Posts: 4050
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The snip of code you've given us will not throw a NullPointer unless it is thrown from myFunction().

Perhaps you can tell us on exactly what line the NullPointer is coming from. Show us the stack trace.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That bit of code can't be causing your problem. Here's a SSCCE that shows it works:



Can you post a SSCCE of your own that generates the error message?
 
Mike Cheung
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I was scratching my head too! It turns out it was because inside myFunc() there was a .toString() call on a variable that is null.
This however brings upon a second question. Any idea why following is the case? First way of checking whether value is null is not working as expected when myFunc() returns null.
 
Tim Cooke
Marshal
Posts: 4050
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you've been bitten by how java does string equality. Search the forum for "string equality" or "string comparison" and you'll find it's been discussed many times before. I even think it's the first topic on the Ranch FAQ.
 
Mike Cheung
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks!
 
Tim Cooke
Marshal
Posts: 4050
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FAQ article is this one: Avoid The Equality Operator
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This way of comparing variable to null is a bad idea. Even when you change != to equals() the result will be invalid in one case:

First the code:Then the hint-question.
What happens if myFunc() returns "null"? (note that I mean "null" as a String object with value "null", not null as null reference).
 
Mike Cheung
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I'm not following because if it's returning a String null then is still a String and the.equals() should work. But apparently there is something I'm missing here. Not sure what it is.
 
Tim Cooke
Marshal
Posts: 4050
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel, I don't see what you're getting at either. Care to elaborate?
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Pawel, I don't see what you're getting at either. Care to elaborate?

Sure.
In the post starting with: Thanks! I was scratching my head too!
OP used following technique to check if a string is null (the result of myFunc()).
I'll put it in method:This code works as expected for almost all cases... Except when you have a String with value "null".
Then isNull("null") returns true. And this is wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!