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?
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)
.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.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
student
, but you forgot to initialize it like in the correct code shown below: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
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:
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.)