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?
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 anint
and Java will initialize it to0
for you. When you assign it the value of10
on the second line, your value of10
is written into the memory location referred to byx
.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 actually contain a primitive value yet. Instead, it contains a pointer (because the type isInteger
which is a reference type). Since you have not yet said what to point to, Java sets it tonull
, which means "I am pointing to nothing".In the second line, the
new
keyword is used to instantiate (or create) an object of typeInteger
and the pointer variablenum
is assigned to thatInteger
object.The
NullPointerException
occurs when you declare a variable but did not create an object. So you are pointing to something that does not actually exist.If you attempt to dereference
num
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 may 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
, but rather assuming that it was created before thedoSomething()
method was called. Note, it is possible to call the method like this:In which case,
obj
isnull
. 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
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
.A lot of explanations are already present to explain how it happens and how to fix it, but you should also follow best practices to avoid
NullPointerException
s at all.See also: A good list of best practices
I would add, very important, make a good use of the
final
modifier. Using the "final" modifier whenever applicable in JavaSummary:
final
modifier to enforce good initialization.@NotNull
and@Nullable
if("knownObject".equals(unknownObject)
valueOf()
over toString().StringUtils
methodsStringUtils.isEmpty(null)
.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
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.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: