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?
Probably the quickest example code I could come up with to illustrate a
On the first line inside
(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 caused when you dereference a variable that is pointing to
In general, it's because something hasn't been initialized properly.
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
In this example, the variable x is an
But, when you try to declare a reference type something different happens. Take the following code:
The first line declares a variable named
In the second line, the
For instance, you may have a method as follows:
in which case you are not creating the object
in which case
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,
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 an important thing to know - when there are no more references to an object (in the example above when
In Java, everything is in the form of a class.
If you want to use any object then you have two phases:
Same for the array concept
If you are not giving the initialization section then the
A null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include:
Applications should throw instances of this class to indicate other illegal uses of the
A null pointer exception is an indicator that you are using an object without initializing it.
For example, below is a student class which will use it in our code.
The below code gives you a null pointer exception.
Because you are using
Another occurrence of a
This particular NPE can be avoided if the comparison order is reversed; namely, use
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
You must initialize the elements in the array before accessing or dereferencing them.
What is a NullPointerException?
A good place to start is the JavaDocs. They have this covered:
It is also the case that if you attempt to use a null reference with
How do I fix it?
So you have a
Identify the null values
The first step is identifying exactly which values are causing the exception. For this, we need to do some debugging. It's important to learn to read a stacktrace. This will show you where the exception was thrown:
Here, we see that the exception is thrown on line 13 (in the
Trace where these values come from
Next check where this value comes from. By following the callers of the method, we see that
Trace where these values should be set
This is enough to give us a solution: add a call to
The variable can have a default value (and
Or you can design the class so that
I still can't find the problem
If you tried to debug the problem and still don't have a solution, you can post a question for more help, but make sure to include what you've tried so far. At a minimum, include the stacktrace in the question, and mark the important line numbers in the code. Also, try simplifying the code first (see SSCCE).
Question: What causes a
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
See also: A good list of best practices
I would add, very important, make a good use of the