Avoid Null Pointer Exception in Java

Null Pointer Exception is the most common and most annoying exception in Java. In this post I want to avoid this undesired exception.

First let’s create example that raise Null Pointer Exception

private Boolean isFinished(String status) {  
     if (status.equalsIgnoreCase("Finish")) {  
       return Boolean.TRUE;  
     } else {  
       return Boolean.FALSE;  
     }  
   }

In previous method if we pass the value of “status” variable as null it will raise Null Pointer Exception in below line

if (status.equalsIgnoreCase("Finish")) {

So we should change my code to below code to avoid Null Pointer Exception

private Boolean isFinished(String status) {
        if ("Finish".equalsIgnoreCase(status)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

In previous method if we path the value of “status” variable as null it will not raise Null Pointer Exception.

If you have object.equals(”literal”) you should replace with “literal”.equals(object) .

If you have object.equals(Enum.enumElement) you should replace with

Enum.enumElement.equals(object).

At general expose equals method of the object that you are sure that it doesn’t has null value.

I will continue in providing more best practice and advices.

In part 1 post I listed how to avoid NPE in equalsIgnoreCase() method and enumerator, today I will write about below cases
1- Empty Collection
2- Use some Methods
3- assert Keyword
4- Assert Class
5- Exception Handling
6- Too many dot syntax
7- StringUtils Class

1- Empty Collection

Empty collection is collection which hasn’t any elements. Some developers return null value for Collection which has no elements but this is false, you should return Collections.EMPTY_LIST, Collections.EMPTY_SET and Collections.EMPTY_MAP.

Wrong Code

  public static List getEmployees() {  
     List list = null;  
     return list;  
   }  

Correct Code

   public static List getEmployees() {  
     List list = Collections.EMPTY_LIST;  
     return list;  
   }  

2- Use some Method

Use some methods to assure that null value not exist like contains(), indexOf(), isEmpty(), containsKey(), containsValue() and hasNext().

Example

     String myName = "Mahmoud A. El-Sayed";  
       
     List list = Collections.EMPTY_LIST;  
     boolean exist = list.contains(myName);  
     int index = list.indexOf(myName);  
     boolean isEmpty =list.isEmpty();  
       
     Map map =Collections.EMPTY_MAP;  
     exist=map.containsKey(myName);  
     exist=map.containsValue(myName);  
     isEmpty=map.isEmpty();  
       
     Set set=Collections.EMPTY_SET;  
     exist=set.contains(myName);  
     isEmpty=set.isEmpty();  
       
     Iterator iterator;  
     exist = iterator.hasNext();  

3- assert Keyword

assert is keyword provided in Java 1.4 which enables you to test your assumptions about your code.

Syntax of assert keyword

 assert expression1 ;  

expression1 is boolean expression which is evaluated and if it is false system will throw AssertionError with no detail message

assert expression1 : expression2 ;  

expression1 is boolean expression which is evaluated and if it is false system will throw AssertionError and detail message is expression2

For example in my post I want to assert that expression is not null then I should write below code

   public static String getManager(String employeeId) {  
     assert (employeeId != null) : "employeeId must be not null";  
     return "Mahmoud A. El-Sayed";  
   } 

If I try to call getManager method using getManager(null);
It will raise “java.lang.AssertionError: employeeId must be not null”

Note use -enableassertion in your java option while run your code to enable assertion.

4- Assert Class

Assert class exists in com.bea.core.repackaged.springframework.util package and has a lot of methods used in assertion.

Example

   public static String getManager(String employeeId) {  
     Assert.notNull(employeeId, "employeeId must be not null");  
     Assert.hasLength(employeeId, "employeeId must has length greater than 0");  
     return "Mahmoud A. El-Sayed";  
   }  

If I try to call getManager method using getManager(null);
It will raise “java.lang.IllegalArgumentException: employeeId must be not null”

5- Exception Handling

I should take care in exception handling using try catch statement or checking of null value of variables
For example

   public static String getManager(String employeeId) {  
     return null;  
   } 

I will cal it using below code

     String managerId = getManager("A015");  
     System.out.println(managerId.toString());

It will raise “java.lang.NullPointerException” , so to handle this exception I should use try catch or checking of null values

a- try catch statement

I will change calling code to below code

     String managerId = getManager("A015");  
     try {  
       System.out.println(managerId.toString());  
     } catch (NullPointerException npe) {  
       //write your code here  
     }  

b- Checking of Null values

I will change calling code to below code

     String managerId = getManager("A015");  
     if (managerId != null) {  
       System.out.println(managerId.toString());  
     } else {  
       //write your code here  
     }  

6- Too many dot syntax

Some developers use this approach as he writes less code but in the future will not be easier for maintenance and handling exception

Wrong Code

 String attrValue = (String)findViewObject("VO_NAME").getCurrentRow().getAttribute("Attribute_NAME");

Correct Code

     ViewObject vo = findViewObject("VO_NAME");  
     Row row = vo.getCurrentRow();  
     String attrValue = (String)row.getAttribute("Attribute_NAME");

7- StringUtils Class

StringUtils class is part of org.apache.commons.lang package, I can use it to avoid NPE specially all its methods are null safe

For example StringUtils. IsEmpty(), StringUtils. IsBlank(), StringUtils.equals(), and much more.
You can read specification of this class from here

Conclusion Always take care of NullPointerException when writing code and guess how it will be thrown in your code and write //TODO in your code for solving it later if you haven’t more time.

Reference: Avoid Null Pointer Exception Part 1Avoid Null Pointer Exception Part 2 from our JCG partner Mahmoud A. ElSayed at the Dive in Oracle blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

8 Responses to "Avoid Null Pointer Exception in Java"

  1. Defensive programming is a powerful tool that you should use with care. Sure thing you must be defensive. But be very careful that you do not alter behavior of a function or class just because an object is null. If you get a null object that is not allowed, a null-pointer is quite a good indication of what’s wrong and definitely better than IllegalArgumentException.

    In non-public APIs, adding null-checks and adding special behavior for null-objects (like throwing other exceptions, or logging and continuing in another branch) is a smell that you don’t trust you team for properly testing their code. If that’s true, consider TDD as it will help you to gain more trust.

    You’re right that you should avoid train wrecks (too-many-dot-syntax). Following OO principles, you should only call functions on objects the function creates, objects the function gets passed, the function’s class, and fields in your class. Following this, eliminates train wrecks and promotes the Open-Closed principle.

    By the way, putting TODO comments in your code doesn’t make things more defensive. In fact it makes things just a bit worse because it shows that you’re in a rush, and take no time to craft your code the way it deserves.

  2. Robert Ryals says:

    The Guava Optional class is an excellant way to avoid NPE’s as well.

    http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html

  3. Returning null puts workload on the users of your API so it should be avoided. The too-many-dots approach should be avoid because you are having too much knowledge of the object graph in one method and this is against OOP principles, making changes difficult to made, as you said. But your correct code is the same but with variables, a much better way is to move that code to its own method in the View object and use that instead of navigate the object graph every time. The try-catch approach is like killing a mosquito with a bazooka. 

  4. 1. You are talking about avoiding NullPointerException while you return Boolean (nullable object) where simple boolean (primitive) is sufficient.

    2. Collections.emptyList() is preferred due to better handling of generics.

    3. com.bea.core.repackaged.springframework.util – what? Why not use assertions in Guava or Apache Commons Lang?

    4. I have never seen legitimate use case for catching NullPointerException, please do not encourage to do so.

  5. Gürkan İndibay says:

    Good article. Thank you for the information

    At number 5, Handling Exception, You said that catching null pointer exception is an alternative for null check.
    However Catching NullPointerException is not a good practice. Doing null chek is better than this alternative.

Leave a Reply


9 × = eighteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close