Last Updated:

What is a NullPointerException and How to Fix It - Java

Quite often, when developing in Java, programmers encounter a NullPointerException that appears in the most unexpected places. In this article, we will figure out how to fix this and how to try to avoid the appearance of NPE in the future.

NullPointerException – what is it?

 

A nullPointerException (aka NPE) is an exception that is thrown whenever you access an object's method or field by reference, which is null. Let's look at a simple example:

 

Integer n1 = null;
System. out. println(n1. toString());

Here, on the first line, we declared a variable of type Integer and set it to null (that is, the variable does not point to any existing object).


On the second line, we access the toString method of the variable n1. Because the variable is null, the method cannot execute (the variable does not point to any real object), a NullPointerException is thrown:

 

Exception in thread "main" java.lang.NullPointerException
at ru.javalessons.errors.NPEExample.main(NPEExample.java:6)

How to Fix NullPointerException

In our simplest example, we can fix npe by assigning an object (i.e. not null) to the variable n1:

 

Integer n1 = 16;
System. out. println(n1. toString());

Now there will be no exception when accessing the toString method and our program will work correctly.

If your program crashed due to a NullPointerException (or you intercepted it somewhere), you need to determine from the thectrace which line of source code caused the exception to appear. Sometimes the cause is localized and corrected very quickly, in non-trivial cases you need to determine where the code is previously assigned a null value.

Sometimes you need to use debugging and step-by-step through the program to determine the source of the NPE.

How to avoid a NullPointerException

 

There are many techniques and tools to avoid the appearance of NullPointerException. Consider the most popular of them.

Check for null all objects that are not created by you

 

If the object is not created by you, sometimes it is worth checking for null to avoid situations with NullPinterException. Here the main thing is to determine for yourself the framework in which the object is considered "correct" and still "incorrect" (that is, invalidated).

Do not believe the incoming data

 

If you receive data from someone else's source (a response from some external service, reading from a file, user input), do not believe this data. This principle is applied more broadly than just detecting NPE errors, but it is possible and necessary to detect NPE at this stage. Check objects for null. In a broader sense, check the data for correctness, and consistency.

Return existing objects instead of null

 

If you create a method that returns a collection of objects – do not return null, return an empty collection. If you return a single object, it is sometimes convenient to use the Optional class (introduced in Java 8).

Conclusion

 

In this article, we described how to fix NullPointerException situations and how to effectively prevent such situations when developing programs.