This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between printing null and getting null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between printing null and getting null pointer exception" Watch "Difference between printing null and getting null pointer exception" New topic
Author

Difference between printing null and getting null pointer exception

Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

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

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Feb 12, 2013
Posts: 33

That should do it. All clear now. Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between printing null and getting null pointer exception
 
Similar Threads
double Type of Value in JTable
SCJP Brainteaser (2)
problem in running exec() from servlet
explain me this code
How to play audio using this code in Grid View