Best way to handle the null pointer exceptions

What is the best way to handle the null pointer exceptions? Null is a common enemy to all the developers and the application. Most of the NPEs are caused by incorrectly set up mocks during development. These below techniques will improve your Java language along with the best way to handle the null pointer exceptions in productions.

  • Improving Null Safety in Java 8  by using an optional class.

A null reference is the source of many problems because it is often used to denote the absence of a value. Java SE 8 introduces a new class called java.util.Optional that can resolve some of these problems.  But this class won’t give the 100% solution for the null issue. Let’s discuss below when to use optional class for a null check.

Let’s take an example of below a class.


This code looks pretty reasonable. A common (bad) practice is to return the null reference to indicate the absence of an Inner. Unfortunately, this means the call to getInner() will try to return the Inner object of a null reference, which will result in a NullPointerException at runtime and stop your program from running further.

We have to write a bunch of null checks to make sure not to throw a NullPointerException in run time:

As you see above code, Resolving a deeply nested path in this structure can be kind of awkward. We can get rid of all those null checks by utilizing the Java 8 Optional type. Null checks are automatically handled under the hood.

Optional type is best for resolving nested references. If it is one level of null check then uses it directly by if condition.

  • Return Empty Collections Instead of Null

Never create any collections with a null value because already know that null is not the best return value of a method. Since a collection can contain any amount of elements, it can also contain 0 elements. Better to return an empty collection instead of null.

  • Validate Public exposed API Arguments

For the Public exposed API for your application, there is no control over the users what they pass your API to request your API. Always best practice validating the user request by using below way.

  • Never Pass Null as an Argument

This is one of the most important principles of good coding. Passing null to indicate that there’s no value for a given argument might seem like a viable option. But it has two big disadvantages:

  • You need to read the function’s implementation and figure out if it, and potentially every affected function down the way, can handle null value correctly.
  • You have to always be careful when changing function’s implementation not to throw away something that might handle nulls for its users. Otherwise, you have to search through the whole source code to check if null is being passed anywhere.

By accepting the principle of never passing null, both of these problems are gone forever.

Leave a Reply

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

Show Buttons
Hide Buttons