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.)
Question: What causes a
NullPointerException
(NPE)?As you should know, Java types are divided into primitive types (
boolean
,int
, etc.) and reference types. Reference types in Java allow you to use the special valuenull
which is the Java way of saying "no object".A
NullPointerException
is thrown at runtime whenever your program attempts to use anull
as if it was a real reference. For example, if you write this:the statement labelled "HERE" is going to attempt to run the
length()
method on anull
reference, and this will throw aNullPointerException
.There are many ways that you could use a
null
value that will result in aNullPointerException
. In fact, the only things that you can do with anull
without causing an NPE are:==
or!=
operators, orinstanceof
.Question: How do I read the NPE stacktrace?
Suppose that I compile and run the program above:
First observation: the compilation succeeds! The problem in the program is NOT a compilation error. It is a runtime error. (Some IDEs may warn your program will always throw an exception ... but the standard
javac
compiler doesn't.)Second observation: when I run the program, it outputs two lines of "gobbledy-gook". WRONG!! That's not gobbledy-gook. It is a stacktrace ... and it provides vital information that will help you track down the error in your code, if you take the time to read it carefully.
So let's look at what it says:
The first line of the stack trace tells you a number of things:
java.lang.NullPointerException
.NullPointerException
is unusual in this respect, because it rarely has an error message.The second line is the most important one in diagnosing an NPE.
This tells us a number of things:
main
method of theTest
class.If you count the lines in the file above, line 4 is the one that I labeled with the "HERE" comment.
Note that in a more complicated example, there will be lots of lines in the NPE stack trace. But you can be sure that the second line (the first "at" line) will tell you where the NPE was thrown1.
In short the stack trace will tell us unambiguously which statement of the program has thrown the NPE.
1 - Not quite true. There are things called nested exceptions...
Question: How do I track down the cause of the NPE exception in my code?
This is the hard part. The short answer is to apply logical inference to the evidence provided by the stack trace, the source code and the relevant API documentation.
Let's illustrate with the simple example (above) first. We start by looking at the line that the stack trace has told us is where the NPE happened:
How can that throw an NPE?
In fact there is only one way: it can only happen if
foo
has the valuenull
. We then try to run thelength()
method onnull
and .... BANG!But (I hear you say) what if the NPE was thrown inside the
length()
method call?Well, if that happened, the stack trace would look different. The first "at" line would say that the exception was thrown in some line in the
java.lang.String
class, and line 4 ofTest.java
would be the second "at" line.So where did that
null
come from? In this case it is obvious, and it is obvious what we need to do to fix it. (Assign a non-null value tofoo
.)OK, so let's try a slightly more tricky example. This will require some logical deduction.
So now we have two "at" lines. The first one is for this line:
and the second one is for this line:
Looking at the first line, how could that throw an NPE? There are two ways:
bar
isnull
thenbar[pos]
will throw an NPE.bar[pos]
isnull
then callinglength()
on it will throw an NPE.Next, we need to figure out which of those scenarios explains what is actually happening. We will start by exploring the first one:
Where does
bar
come from? It is a parameter to thetest
method call, and if we look at howtest
was called, we can see that it comes from thefoo
static variable. In addition, we can see clearly that we initializedfoo
to a non-null value. That is sufficient to tentatively dismiss this explanation. (In theory, something else could changefoo
tonull
... but that is not happening here.)So what about our second scenario? Well, we can see that
pos
is1
, so that means thatfoo[1]
must benull
. Is that possible?Indeed it is! And that is the problem. When we initialize like this:
we allocate a
String[]
with two elements that are initialized tonull
. After that, we have not changed the contents offoo
... sofoo[1]
will still benull
.When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive type
int
:In this example, the variable x is an
int
and Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x.But, when you try to declare a reference type something different happens. Take the following code:
The first line declares a variable named
num
, but, it does not contain a primitive value. Instead, it contains a pointer (because the type isInteger
which is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".In the second line, the
new
keyword is used to instantiate (or create) an object of type Integer and the pointer variablenum
is assigned this object. You can now reference the object using the dereferencing operator.
(a dot).The
Exception
that you asked about occurs when you declare a variable but did not create an object. If you attempt to dereferencenum
BEFORE creating the object you get aNullPointerException
. In the most trivial cases, the compiler will catch the problem and let you know that "num may not have been initialized" but sometimes you write code that does not directly create the object.For instance, you may have a method as follows:
In which case you are not creating the object
obj
, rather assuming that it was created before thedoSomething
method was called. Unfortunately, it is possible to call the method like this:In which case
obj
is null. If the method is intended to do something to the passed-in object, it is appropriate to throw theNullPointerException
because it's a programmer error and the programmer will need that information for debugging purposes.Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example,
doSomething
could be written as:Finally, How to pinpoint the exception & cause using Stack Trace
Another occurrence of a
NullPointerException
occurs when one declares an object array, then immediately tries to dereference elements inside of it.This particular NPE can be avoided if the comparison order is reversed; namely, use
.equals
on a guaranteed non-null object.All elements inside of an array are initialized to their common initial value; for any type of object array, that means that all elements are
null
.You must initialize the elements in the array before accessing or dereferencing them.
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 a null 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.