Wednesday, December 11, 2013

[Java] What is NullPointerException

A NullPointerException occurs when a null reference variable is used to access the members of an object. it is a common runtime error which is a class extends RuntimeException.  NullPointerException occurs when you invoke a method on a reference variable with a null value. and is would be thrown when an application attempts to use null in a case where an object is required. These include:
  1. Calling the instance method of a null object.
  2. Accessing or modifying the field of a null object.
  3. Taking the length of null as if it were an array.
  4. Accessing or modifying the slots of null as if it were an array.
  5. 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. NullPointerException objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.

Make sure you assign an object reference to the variable before invoking the method through the reference variable to avoid NullPointerException.

Remarks
Text provided above does not means is the model answer, and it's was posted here for an reference to somebody who interested the related knowledge only. if there is something incorrect, please leave a comment so that i can correct it or for others viewer to have an better reference, if you want know more about this topic and where is the question, concept or answer from, you can reference from information provided under the reference sub-title.

Reference:
Introduction to Java Programming, Y. Daniel Liang
http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

No comments :

Post a Comment