Exception HandlingJavaProgramming

Good Bye NullPointerException in Java

3 Mins read

Null Pointer Exception is the worst nightmare of a java programmer. I have faced this many times and learned it the hard way.

This becomes even more difficult when you are maintaining a legacy code and have little idea about history of implementation choices that were made before you started working.

I have worked on many java applications including legacy and fresh developed.

Null pointer Exception can be really common at applications that do not have unit testing.

What is NullPointerException ?

This is a runtime exception in Java. It is thrown when a operation is called on a null object. Many Java API classes throw (some intentional / some unintentional) this exception to ensure the expected input value is not null.

When NullPointerException Can be thrown?

Many API throw null pointer exception to ensure the client pass valid input. Depending on a scenario Null values may not be acceptable for some APIs. This can be handled in several ways.

  • Silently ignore the value – This may be a valid solution in some cases. However, it may not work always. If client is expecting a result by providing a invalid input you may want to clearly define this in your method.
  • Throw NullPointerException – this is most common approach for Java libraries and core java API. This is also a default behavior of java. Any method invocation on null object will result in NPE.
  • Validate Input and Throw specific exceptions or error – This may be suitable for specific domain specific exception and error handling.

How To Avoid NullPointerExceptions in Java 9?

If you are using Java 9 – this has become very simple. You can Object.requireNonNullElse method with a default value.

This is actually my favorite enhancement in Java 9.

Though this feature does not guarantee that your code will never have a NullPointerException it is still a big relief. This small feature is going to save thousands of unwanted lines written by Java developers for null safety.

How To Avoid NullpointerExceptions in Java 8 and below?

If you are not using Java 9 yet, you may want to try below options to avoid it. These options are still going to depend on developer discipline and code review.

I have been able to reach to zero null pointer exception goal in past few months with the help of Eclipse, IntelliJ and FindBugs.

Finding NPE with Eclipse

Eclipse has Null analysis feature for java projects that can warn you at the time of development. The default setup of eclipse is set to ignore this, however you can set it to warning level.

Windows > Preferences > Java > Compiler > Errors/Warnings > Potential Programming Problems > Null Analysis

Finding Potential NPE with IntelliJ Code Inspect

IntelliJ is really powerful IDE and has really good code inspection feature inbuilt in the open source community version.

This code inspection is very much like FindBugs and many of the suggestions are not critical to app.

My favorite code inspection section is “Probable Bugs”

This section can unfold a lot of mistakes that go unnoticed. You will find a NullPointerException warning in this section.

This is really easy to do with few clicks. Follow below mentioned steps to start fixing your NPE

  • Right click on the file or folder
  • Select Analyze
  • Click Inspect Code

  • Specify Inspection Scope 
    • You can edit the types of rules to run using the ellipses button. 
    • Uncheck test sources
    • Hit OK
  • Your report will be shown and a lot of autofix help is already their to quickly fix one file or whole project. 

Below is a snapshot of Inspections dialog with various inspections including NullPointer check.

Finding NPE with Findbugs Report

Findbugs is a good friend of developer to point out any potential issues including NPE.

Find bugs provides you multiple integration points to catch NPE.

  • Eclipse Plugin
  • Maven Plugin
  • Jenkins Plugin

I like the Eclipse Plugin for Findbugs during development. It is available via the eclipse update site.

Once you have the plugin installed, you can right click on any source file run the finbugs in Eclipse

Why Findbugs Is Awesome? 

Finbugs has helped find many bugs including null pointers. There are many tools that may give you false positives. Findbugs has better quality results and any high priority findbugs is  recommended to be fixed.

An interesting whitepaper from Finbugs about NPE : Finding More Null Pointer Bugs, But Not Too Many


Hope you will find these methods useful in reducing NullPointerException occurrence in your software. Please share in comments about any ideas you have. 

Leave a Reply

Your email address will not be published. Required fields are marked *