Remove Dirty NullChecks in Java

Sivaram Rasathurai
Javarevisited
Published in
2 min readJan 22, 2022

--

When I code something in Java, I am always worrying about NullPointerException.

As a developer, I should provide error-free code.

For some projects, I have worked in Javascript, I saw one beautiful/amazing thing in javascript. That is we can check to execute some function if the value is not null only, otherwise, it will not execute.

let name = user ?. name;

This ?. is technically equivalent to

(user !== null || user !== undefined)             ? user.name             : undefined;

Can’t we do this in Java?

We can do with creating some generic methods as follow.

public static <I, O> O executeViaNullSafer(I value, Function<I, O> executorFunction) {
return value != null ? executorFunction.apply(value) : null;
}

This function receives value and an executable function. If the value is not null the function will execute and the output of that executable function will be returned from the method otherwise the method will return null.

This is a single line method that can act like a javascript ?. operator.

How?

For example, If I need to trim some String type variable. Obviously, we need to check the variable is null or not before the trim because If the variable is null then It will provide a NullPointerException.

Normal code

public String trimValue(String value){
if(value==null){
return null;
}else{
return value.trim();
}

We can improve this code with modern java as follow.

public String trimValue(String value){
return value==null) ? null : value.trim();

Let’s try our executeViaNullSafer Method

String trimmedValue = executeViaNullSafer(value, val -> val.trim());

We can use Method Reference as well

String trimmedValue = executeViaNullSafer(value,String::trim);

Let’s look for the javascript example

String name = executeViaNullSafer(user, User::getName);

This is how I reduced these null checks in my java application.
I published NullUtil maven dependency as well.

https://search.maven.org/artifact/io.github.rcvaram/NullUtil/1.0/jar

<dependency>
<groupId>io.github.rcvaram</groupId
<artifactId>NullUtil</artifactId>
<version>1.0</version>
</dependency>

Github URL → https://github.com/rcvaram/NullUtil

What else this dependency does for you.

  • NullUtil.executeViaNullSafer method will execute the given function on Non-Null Values and, when the value is null It will return Null instead of executing the function.
  • NullUtil.executeExecutorOrDefault method will execute the given function on Non-Null Values and, If the value is null, It will return the given default value.
  • NullUtil.executeMutator method mutates the reference type values without returning anything. If the value is null, It will not do anything.
  • NullUtil.replaceNull method will replace the null value with the given defaultValue. when the value is not null, It will return the value.
  • NullUtil.hasText method is used to check the String value has any kind of text or not.
  • NullUtil.trimValue method will be used to trim the String without causing the null pointer Exception.

--

--