This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes  null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark " null pointer exception " Watch " null pointer exception " New topic
Author

null pointer exception

Sonia Kapoor
Greenhorn

Joined: Nov 01, 2005
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24189
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Sonia Kapoor
Greenhorn

Joined: Nov 01, 2005
Posts: 3
Thank you!
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: null pointer exception
 
It's not a secret anymore!