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 ( What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely? |
||||
locked by Robert Harvey♦ Aug 26 '14 at 23:04This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here |
||||
|
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 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 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,
Finally, How to pinpoint the exception location & cause using Stack Trace |
|||||||||||||||||||||
|
Probably the quickest example code I could come up with to illustrate a
On the first line inside main I'm explicitly setting the (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.) |
|||||||||||||||||||||
|
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 printString method). Look at line and check which values are null by
adding logging statements or using a debugger. We find out that 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 Where is This is enough to give us a solution: add a call to Other fixesThe variable can have a default value (and
Either the
Or you can design the class so that
See also: I still can't find the problemIf 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 null pointer exception is caused when you dereference a variable that is pointing to
|
|||||||||||||||||||||
|
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 is one that points to nowhere. When you dereference a pointer "p", you say "give me the data at the location stored in "p". When p is a null pointer, the location stored in "p" is "nowhere", you're saying "give me the data at the location 'nowhere'". Obviously it can't do this, so it throws a NULL pointer exception. In general, it's because something hasn't been initialized properly. |
||||
|
In Java every things is in the form of class. If you want to use any object then you have two phases
Example:
Same for Array concept
If you not given Initialization section then the |
|||||
|
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 ask 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 "reference" and "otherReference" point to null) then the object is "unreachable". There is no way we can work with it, so this object is marked for to be garbage collected, and at some point the VM will free the memory used by this object and will allocate another. |
||||
|
A null pointer exception is an indicator that you are using Object without initialize it. e.g below is a student class which will use in our code.
below code give you null pointer exception .
Because you are using
|
|||||||||||||
|
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 A good list of best practices is for example here: I would add, very important, make a good use of the Summary:
|
|||||||||
|
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 derefencing them.
|
|||||||||
|