@NonNull

How To Use @NonNull Annotation With Project Lombok in Java Applications

@NonNull annotation enables you to avoid having to manually write code to check if method or constructor parameters are null.

# Dev Environment:

  • Apache maven (v3.6.1)
  • maven-compiler-plugin (v3.8.1)
  • lombok (v1.18.8)
  • Apache Netbeans (v10.0)

TIP: For those who would like to follow along with this blog, just clone my repository git clone https://github.com/steven7mwesigwa/java-tutorials.git and navigate to java-tutorials\project-lombok\nonnull. In there, you should be able to play around with all the source code used in this blog post.

Normally, you would have to write code to check if a passed in argument is null. i.e


        if (param == null) {
            throw new NullPointerException("param is marked @NonNull but is null");
        }

But with lombok, all we need is to add @NonNull right infront of a parameter.

After this, lombok will automatically generate a null-check statement right inside your own method or constructor.

To give you a better understanding of this concept, let's demonstrate this with an example.

# Demonstration : (Without Lombok)

Let's create a Person class that will have a manual implementation of a null-check statement. Full code here


//demo2
package com.stevenmwesigwa.nonnull.demo2;

public class Person {

    private String firstname;
    private String lastname;
    private String ssn;

    public Person(String firstname, String lastname, String ssn) {
        if (ssn == null) {
            throw new NullPointerException("ssn is marked @NonNull but is null");
        }
        this.firstname = firstname;
        this.lastname = lastname;
        this.ssn = ssn;
    }

    @Override
    public String toString() {
        return "Person{" + "firstname=" + firstname + ", lastname=" + lastname + ", ssn=" + ssn + '}';
    }

}

As you would notice, we're placing a null-check right inside our constructor to make sure that the client provides a social security number(ssn) while creating a Person object.

When we create and run an App.java class that makes use of this Person class Full code here but pass in null for ssn, we receive the error below.

		
...

--- exec-maven-plugin:1.5.0:exec (default-cli) @ nonnull ---
Exception in thread "main" java.lang.NullPointerException: ssn is marked @NonNull but is null
	at com.stevenmwesigwa.nonnull.demo2.Person.(Person.java:15)
	at com.stevenmwesigwa.nonnull.demo2.App.main(App.java:11)
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
		
...		

This basically is intended to throw a NullPointerException incase the client doesn't provide a non-null value for ssn.

Now getting to the meat and potatoes of this blog, let's take a look at how we can leverage the power of lombok library to have it auto generate null-check statements for us.

# Demonstration : (With Lombok)

Let's create a Person class but this time making use of @NonNull annotation... Full code here

//demo1

package com.stevenmwesigwa.nonnull.demo1;

import lombok.NonNull;

public class Person {

    private String firstname;
    private String lastname;
    private String ssn;

    public Person(String firstname, String lastname, @NonNull String ssn) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.ssn = ssn;
    }

    @Override
    public String toString() {
        return "Person{" + "firstname=" + firstname + ", lastname=" + lastname + ", ssn=" + ssn + '}';
    }

}

By prepending a @NonNull annotation on ssn parameter in the constructor, lombok would automatically generate the null-check statement for us.

NOTE: You're free to add this annotation to any method or constructor parameter.

When we create and run an App.java class that makes use of this Person class Full code here but pass in null for ssn, we receive the same error like we received before.

If you've reached this far, i would assume you've learned something today.

Don't hesitate to leave a comment. Your feedback is greatly appreciated.

Sharing is caring. Share this blog to help out others getting started with 'project lombok' library for Java applications.

About The Author   

Steven Mwesigwa

Software Engineer at Vogue Book Consultancy Services Ltd


Picture Of Steven Mwesigwa

Steve is currently a software developer at Vogue Book Consultancy Services Ltd and a technology author. He holds a Dip. in civil engineering from Kyambogo University. He founded and maintains stevenmwesigwa.com a website that receives more than 1.5 thousand visits per month. Steve can be reached on Twitter at @steven7mwwesigwa