• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Function returning null crashes code when assigned to variable

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thanks!
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The FAQ article is this one: Avoid The Equality Operator
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pawel, I don't see what you're getting at either. Care to elaborate?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic