Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception

 
Sonia Kapoor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to understand why this piece of code does not give a null pointer exception:
String test = null;
test.valueOf(someDoubleValue);
someDoubleValue is any double number. Is this because valueOf() is a static method?
Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sonia Kapoor:
Is this because valueOf() is a static method?


Yes. As you know, an object isn't needed to call the static method. The compiler generates code here that never actually uses the variable "test" -- the compiled code just mentions the String class, where the method is defined.

Note that a good IDE (development environment) will warn you here, saying that you're calling a static method via a non-static context -- which is bad style, even though it's not an error.
 
Sonia Kapoor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic