Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

We've been undertaking numerous strategies over the years, from Elvis operators to Optional, to help remove NullPointerExceptions from our apps. In this tutorial, we'll learn about Uber's contribution to the conversation, NullAway, and how to use it.

NullAway is a build tool that helps us to eliminate NullPointerExceptions (NPEs) in our Java code.

This tool performs a series of type-based, local checks to ensure that any pointer that gets dereferenced in your code cannot be null. It has low build-time overhead and can be configured to run in every build of your code.

2. Installation

Let's take a look at how to install NullAway and its dependencies. In this example, we're going to configure NullAway using Gradle.

NullAway is dependant on Error Prone. Therefore, we will add the errorprone plugin:

plugins {
  id "net.ltgt.errorprone" version "1.1.1"
}

We'll also add four dependencies in different scopes: annotationProcessor, compileOnly, errorprone, and errorproneJavac:

dependencies {
  annotationProcessor "com.uber.nullaway:nullaway:0.7.9"
  compileOnly "com.google.code.findbugs:jsr305:3.0.2"
  errorprone "com.google.errorprone:error_prone_core:2.3.4"
  errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
}

Finally, we'll add the Gradle task that configures how NullAway works during the compilation:

import net.ltgt.gradle.errorprone.CheckSeverity

tasks.withType(JavaCompile) {
    options.errorprone {
        check("NullAway", CheckSeverity.ERROR)
        option("NullAway:AnnotatedPackages", "com.baeldung")
    }
}

The above task sets NullAway severity to the error level which means we can configure NullAway to stop the build with an error. By default, NullAway will just warn the user at compile time.

Additionally, the task sets the package to be checked for null dereferences.

And that's it, we're ready now to use the tool in our Java code.

Similarly, we can use other build systems, Maven or Bazel, to integrate the tool.

3. Usage

Let's say we have a Person class, containing an age attribute. Additionally, we have a getAge method that takes a Person instance as a parameter:

Integer getAge(Person person) {
    return person.getAge();
}

At this point, we can see that getAge will throw a NullPointerException if person is null.

NullAway assumes every method parameter, return value, and field are non-null. Therefore, it will expect the person instance to be non-null.

And let's also say that there is someplace in our code that is, indeed, passing a null reference into getAge:

Integer yearsToRetirement() {
    Person p = null;
    // ... p never gets set correctly...
    return 65 - getAge(p);
}

Then, running a build will produce the following error:

error: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
    getAge(p);

We can fix this error by adding a @Nullable annotation to our parameter:

Integer getAge(@Nullable Person person) { 
    // ... same as earlier
}

Now when we run a build, we'll see a new error:

error: [NullAway] dereferenced expression person is @Nullable
    return person.getAge();
            ^

This is telling us that the person instance has the possibility of being null. We can fix this adding a standard null check:

Integer getAge(@Nullable Person person) {
    if (person != null) {
        return person.getAge();
    } else {
        return 0;
    }
}

4. Conclusions

In this tutorial, we've looked at how we can use NullAway to limit the possibilities of encountering NullPointerExceptions.

As always all source code is available on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!