Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between printing null and getting null pointer exception

 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone clearly explain to me when a null is printed in output and a null pointer exception is thrown? I tried reading it in docs.oracle but I didn't quite get the full grasp of it. What is the difference between the two?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tarun

null is a reserve word in Java language. It is used to de-reference an object's reference variable to point to "nothing". Try out the following code and observe the difference between when null is printed and when a NullPointerException is thrown. Let me know if you have any further doubts.

 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Mohandas wrote:Can someone clearly explain to me when a null is printed in output and a null pointer exception is thrown? I tried reading it in docs.oracle but I didn't quite get the full grasp of it. What is the difference between the two?

I think Mansukhdeep's covered most of it, but specifically: the String "null" is returned by the String.valueOf(Object) method, which is what println() uses to convert whatever you supply it (and that is in the docs ).

Winston
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Hi Tarun

null is a reserve word in Java language. It is used to de-reference an object's reference variable to point to "nothing". Try out the following code and observe the difference between when null is printed and when a NullPointerException is thrown. Let me know if you have any further doubts.



So basically, if an object refers to null and if we try to print what that object refers to, null is printed and if we try to access members using that object or try to perform manipulations with it, it throws a NullPointerException. Is that it or is there more to this sentence?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the formal definition from the JavaDocs Tarun:



public class NullPointerException
extends RuntimeException

Thrown when an application attempts to use null in a case where an object is required. These include:

Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.


That should do it. Any further doubts?
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should do it. All clear now. Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic