What are Null Pointer Exceptions (java.lang.NullPointerException
) and what causes them?
What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
What are Null Pointer Exceptions (java.lang.NullPointerException
) and what causes them?
What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
NullPointerException
s are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger aNullPointerException
. These are the most common, but other ways are listed on theNullPointerException
javadoc page.Probably the quickest example code I could come up with to illustrate a
NullPointerException
would be:On the first line inside
main
, I'm explicitly setting theObject
referenceobj
equal tonull
. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in aNullPointerException
because there is no code to execute in the location that the reference is pointing.(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing anywhere, which is subtly different than pointing to a location that happens to be invalid.)
A null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include:
null
object.null
object.null
as if it were an array.null
as if it were an array.null
as if it were a Throwable value.Applications should throw instances of this class to indicate other illegal uses of the
null
object.Reference: http://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html
A
null
pointer is one that points to nowhere. When you dereference a pointerp
, you say "give me the data at the location stored in "p". Whenp
is anull
pointer, the location stored inp
isnowhere
, you're saying "give me the data at the location 'nowhere'". Obviously, it can't do this, so it throws anull pointer exception
.In general, it's because something hasn't been initialized properly.
In Java all the variables you declare are actually "references" to the objects (or primitives) and not the objects themselves.
When you attempt to execute one object method, the reference asks the living object to execute that method. But if the reference is referencing NULL (nothing, zero, void, nada) then there is no way the method gets executed. Then the runtime let you know this by throwing a NullPointerException.
Your reference is "pointing" to null, thus "Null -> Pointer".
The object lives in the VM memory space and the only way to access it is using
this
references. Take this example:And on another place in your code:
This an important thing to know - when there are no more references to an object (in the example above when
reference
andotherReference
both point to null) then the object is "unreachable". There is no way we can work with it, so this object is ready to be garbage collected, and at some point, the VM will free the memory used by this object and will allocate another.It's like you are trying to access an object which is
null
. Consider below example:At this time you have just declared this object but not initialized or instantiated. And whenever you try to access any property or method in it, it will throw
NullPointerException
which makes sense.See this below example as well:
In Java, everything (excluding primitive types) is in the form of a class.
If you want to use any object then you have two phases:
Example:
Object object;
object = new Object();
Same for the array concept:
Item item[] = new Item[5];
item[0] = new Item();
If you are not giving the initialization section then the
NullPointerException
arise.