Java NullPointerException – examples, causes and fixes

Probably one of the most common and annoying exceptions to be encountered by any Java developer is the dreaded “NullPointerException”. A null pointer exception is thrown when an “illegal” referencing of a null object is performed. A “null” object is an object which is not initialized. Let us explore some examples of how a null pointer exception can be caused.

Let us take the PostCategory class for our null pointy examples below:

public class PostCategory {

private String name;

private String description;

public PostCategory() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

 

Situation 1 – null value provided:

This situation usually occurs when the caller of the method provides a null value such as in the example below:

public class NullbeansApplication {

public static void main(String[] args) {

testNull1(null);
}

public static void testNull1(PostCategory category){
System.out.println(category.getDescription());
}
}

 

The main method here calls the testNull1 method with a null value. Inside the method, we try to dereference the category value that was passed to us. However, since the caller provided a null, Java would complain with a null pointer exception

Exception in thread "main" java.lang.NullPointerException
at com.nullbeans.nullbeansserver.NullbeansserverApplication.testNull1(NullbeansserverApplication.java:17)
at com.nullbeans.nullbeansserver.NullbeansserverApplication.main(NullbeansserverApplication.java:13)

Process finished with exit code 1

 

It is a good practice to handle null objects to avoid an application crash, while informing the end user that something went wrong.
Sometimes, obtaining a null value is can be an expected and normal behavior of an application.

For example, let us say the user calls a function that searches the database for a “Category” object. If we find the object, then we would display its contents. Sometimes the user may search using the wrong name or id and we would obtain a null from the database. In that case we would like to inform the user that the object was not found.

public static void main(String[] args) {

PostCategory searchResult = findCategory("news");
if (searchResult == null) {
System.out.println("Category not found!");
} else {
printCategory(searchResult);
}
}

public static void printCategory(PostCategory category) {
System.out.println(category.getDescription());
}

public static PostCategory findCategory(String name) {
//Some search code here...

 

Situation 2 – Object is initialized, but inner variable is null:

This example builds upon the previous one by trying to dereference a null internal variable. Take for example the “description” member of the PostCategory class. Let us say we want to our program to print out the number of characters in the category description. It would look something like this:

public static void main(String[] args) {

PostCategory category = new PostCategory();
category.setName("News");
printCategory(category);
}

public static void printCategory(PostCategory category){
System.out.println("Category name: "+ category.getName());
System.out.println("Category description: "+ category.getDescription());
System.out.println("Category description size: "+ category.getDescription().length()); //null pointer here
}

 

Running this program would causes it to break when calling the “.lenght()” method, because here tried to dereference a null object, namely the description. The following is the program output:

 

Category name: News
Category description: null
Exception in thread "main" java.lang.NullPointerException
at com.nullbeans.nullbeansserver.NullbeansserverApplication.printCategory(NullbeansserverApplication.java:21)
at com.nullbeans.nullbeansserver.NullbeansserverApplication.main(NullbeansserverApplication.java:15)

Process finished with exit code 1

 

The same situation applies if we access null members of arrays, or even inner members of inner members of inner… of an object. Once has to be careful with handling object references and we need to make sure that if a NullPointerException is possible in one place, that this possibility is taken care off. You never know when data from database or a file could go missing!

Java NullPointerException – examples, causes and fixes
Scroll to top