Doing null checks in Java 8 - A better way.



NullPointerExceptions are like nightmares, most of the time it happens due to a simple null check which we are missing before start using a reference variable which refer to nothing.

We think we all have to deal with carefully while writing even a small piece of java code.
But solving this problem is really simple. Adding a simple if condition which does a condition check.


        
Solving the problem (Imperative).  

As it I mentioned about the best way to avoid this problem is to do a null check before using the object, especially when the object is not created by yourself.

As preventive measure most of time we may need to check the reference of a variable before passing that to a method as an argument.

For an instance bar is an object. Before calling a method foo,We need to check whether variable bar has valid reference or not as shown below.

if(bar != null) {
     obj.foo(bar);
  }

We use this technique everywhere. But things look weird when we repeat this many times in the code. There are situations where we need duplicate this code many times even if the logic is same.  
Something as shown below,

        if(bar1 != null) {
          obj.foo(bar1);
        }
        if(bar2 != null) {
          obj.foo(bar2);
        }

        if(bar3 != null) {
          obj.foo(bar3);
        }

Doing this in a better way (Functional). 

How do we solve this repetition of null check logic? Can we write a method for doing this? Varying components are the variable which we need to do check and the method which we need to call. So those varying components will be the parameters of our new method.
Here is the challenge, How do we parameterize the method call?.
Eg: Passing obj.foo(bar1) as a parameter to a method (Passing a method reference like a variable).

We can solve this problem in more functional way using some of the java 8 features which allow you to pass the function to a function as argument.
The below method can solve this problem.


public <T> void applyIfNotNull(T value, Consumer<T> function){

        if(value != null){

            function.accept(value);

        }

    }



As you can see the above method accept a consumer object and invoke that consumer and pass the value to it only when the variable value has a non-null reference.

Most beautiful part is when we use this method.

We don’t have to write an anonymous inner class for creating a consumer object, instead we can use lambda here.

applyIfNotNull(bar, (val)-> obj.foo(val));

To make it simpler, we can use method reference here.

applyIfNotNull(bar,obj::foo);

For example, this code will print the string value only if it has valid string object reference.

String name = “Steve”;
applyIfNotNull(name, System.out::println);

Conclusion

I am not mentioning few of the other scenarios, what if the method need to return something? How do we handle nested conditions using the same way? etc..  
More than that I see this as a small example which demonstrates, how we can replace common imperative conventional ways with better modern java 8 functional paradigm features.



Comments

Popular posts from this blog

Sorting Objects in Java 8