java.lang.NullPointerException is one of the most popular exceptions in java programming. Anybody working in java must have seen this popping out of nowhere in the java standalone program as well as the java web application.
java.lang.NullPointerException
NullPointerException is a runtime exception, so we don’t need to catch it in the program. NullPointerException is raised in an application when we are trying to do some operation on null where an object is required. Some of the common reasons for NullPointerException in java programs are:
- Invoking a method on an object instance but at runtime the object is null.
- Accessing variables of an object instance that is null at runtime.
- Throwing null in the program
- Accessing index or modifying value of an index of an array that is null
- Checking the length of an array that is null at runtime.
Java NullPointerException Examples
Let’s look at some examples of NullPointerException in java programs.
1. NullPointerException when calling an instance method
When we run the above program, it throws the following NullPointerException error message.
We are getting NullPointerException in the statement t.foo("Hi");
because “t” is null here.
2. Java NullPointerException while accessing/modifying field of a null object
The above program throws the following NullPointerException stack trace.
NullPointerException is being thrown in statement int i = t.x;
because “t” is null here.
3. Java NullPointerException when null is passed in method argument
This is one of the most common occurrences of java.lang.NullPointerException
because it’s the caller who is passing the null argument.
The below image shows the null pointer exception when the above program is executed in Eclipse IDE.
4. java.lang.NullPointerException when null is thrown
Below is the exception stack trace of the above program, showing NullPointerException because of throw null;
statement.
5. NullPointerException when getting the length of null array
6. NullPointerException when accessing index value of null array
7. NullPointerException when synchronized on a null object
The synchronized(mutex)
will throw NullPointerException because the “mutex” object is null.
8. HTTP Status 500 java.lang.NullPointerException
Sometimes we get an error page response from a java web application with an error message as “HTTP Status 500 – Internal Server Error” and root cause as java.lang.NullPointerException
.
For this, I edited the Spring MVC Example project and changed the HomeController method as below.
The below image shows the error message thrown by the web application response.
Here is the exception stack trace:
The root cause is NullPointerException in the statement user.getUserId().toLowerCase()
because user.getUserId()
is returning null.
How to detect java.lang.NullPointerException
Detecting NullPointerException is very easy, just look at the exception trace and it will show you the class name and line number of the exception. Then look at the code and see what could be null causing the NullPointerException. Just look at all the above examples, it’s very clear from stack trace what is causing null pointer exception.
How to fix NullPointerException
java.lang.NullPointerException is an unchecked exception, so we don’t have to catch it. The null pointer exceptions can be prevented using null checks and preventive coding techniques. Look at below code examples showing how to avoid java.lang.NullPointerException
.
Coding Best Practices to avoid NullPointerException
1. Let’s consider the below function and look out for scenario causing null pointer exception.
The NullPointerException can occur if the argument is being passed as null. The same method can be written as below to avoid NullPointerException.
2. We can also add null check for argument and throw IllegalArgumentException
if required.
3. We can use ternary operator as shown in the below example code.
4. Use String.valueOf()
rather than toString()
method. For example check PrintStream println() method code is defined as below.
The below code snippet shows the example where the valueOf() method is used instead of toString().
5. Write methods returning empty objects rather than null wherever possible, for example, empty list, empty string, etc.
6. There are some methods defined in collection classes to avoid NullPointerException, you should use them. For example contains(), containsKey(), and containsValue().
Reference: API Document
Source:
https://www.digitalocean.com/community/tutorials/java-lang-nullpointerexception