Home Lombok Lombok @NonNull annotation Examples

Lombok @NonNull annotation Examples

Lombok @NonNull will generate a null-check for the start of a method or constructor body. Lombok NonNull annotation can be declared on instance fields, methods and constructor arguments.

1. Lombok @NoNull on fields

NonNull annotation on fields will generate null check for starting of setter method body and throws NullPointerException with appropriate exception message. Making primitive fields @NonNull is meaningless, will get compiler warning.

Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.

Lomboked LombokNonNullDemo1.java

@Setter @Getter
public class LombokNonNullDemo1 {

	private Long id;
	
	@NonNull
	private String name;
	
	@NonNull
	private Boolean active;
	
	@NonNull
	private int role;
	
}

Testing Lomboked LombokNonNullDemo1.java

public class LombokNonNullDemo1Test {

	public static void main(String[] args) {
		
		LombokNonNullDemo1 lnd1 = new LombokNonNullDemo1();
		
		lnd1.setId(Long.valueOf(1));
		lnd1.setRole(2);
		lnd1.setName(null);
		lnd1.setActive(true);
		
	}

}

Output Results :

Exception in thread "main" java.lang.NullPointerException: name is marked non-null but is null
	at com.javabydeveloper.lombok.nonnull.LombokNonNullDemo1.setName(LombokNonNullDemo1.java:7)
	at com.javabydeveloper.demo.nonnull.LombokNonNullDemo1Test.main(LombokNonNullDemo1Test.java:13)

DeLomboked LombokNonNullDemo1.java

public class LombokNonNullDemo1 {
	private Long id;
	@NonNull
	private String name;
	@NonNull
	private Boolean active;
	@NonNull
	private int role;

	public void setId(final Long id) {
		this.id = id;
	}

	public void setName(@NonNull final String name) {
		if (name == null) {
			throw new NullPointerException("name is marked non-null but is null");
		}
		this.name = name;
	}

	public void setActive(@NonNull final Boolean active) {
		if (active == null) {
			throw new NullPointerException("active is marked non-null but is null");
		}
		this.active = active;
	}

	public void setRole(@NonNull final int role) {
		this.role = role;
	}

	public Long getId() {
		return this.id;
	}

	@NonNull
	public String getName() {
		return this.name;
	}

	@NonNull
	public Boolean getActive() {
		return this.active;
	}

	@NonNull
	public int getRole() {
		return this.role;
	}
}

2. @NonNull on method arguments

Following example demonstrates how lombok generates code when you use @NonNull on method arguments.

Lomboked LombokNonNullDemo2.java

public class LombokNonNullDemo2 {

	public Double getSurcharge(@NonNull Double transactionAmt) {
		return Math.PI;
	}
	
}

DeLomboked LombokNonNullDemo2.java

public class LombokNonNullDemo2 {
	public Double getSurcharge(@NonNull Double transactionAmt) {
		if (transactionAmt == null) {
			throw new NullPointerException("transactionAmt is marked non-null but is null");
		}
		return Math.PI;
	}
}

3. Lombok @NonNull on Constructor arguments

Following example demonstrates how lombok generates code when you use @NonNull on constructor arguments.

Lomboked LombokNonNullDemo3.java

public class LombokNonNullDemo3 {

	private Long id;
	private String name;
	
	public LombokNonNullDemo3(@NonNull Long id, @NonNull String name) {
		this.id = id;
		this.name = name;
	}
	
}

DeLomboked LombokNonNullDemo3.java

public class LombokNonNullDemo3 {
	private Long id;
	private String name;

	public LombokNonNullDemo3(@NonNull Long id, @NonNull String name) {
		if (id == null) {
			throw new NullPointerException("id is marked non-null but is null");
		}
		if (name == null) {
			throw new NullPointerException("name is marked non-null but is null");
		}
		this.id = id;
		this.name = name;
	}
}

4. Lombok @NoNull with @RequiredArgsConstructor / @AllArgsConstructor

Following example demonstrates how lombok generates code when you use @NonNull with @RequiredArgsConstructor / @AllArgsConstructor annotations.

Lomboked LombokNonNullDemo4.java

@AllArgsConstructor
@RequiredArgsConstructor
public class LombokNonNullDemo4 {

	@NonNull
	private Long id;
	
	private String name;
	
	private final Integer role;
	
	@NonNull
	private Boolean status;
		
}

DeLomboked LombokNonNullDemo4.java

public class LombokNonNullDemo4 {
	@NonNull
	private Long id;
	private String name;
	private final Integer role;
	@NonNull
	private Boolean status;

	public LombokNonNullDemo4(@NonNull final Long id, final String name, final Integer role,
			@NonNull final Boolean status) {
		if (id == null) {
			throw new NullPointerException("id is marked non-null but is null");
		}
		if (status == null) {
			throw new NullPointerException("status is marked non-null but is null");
		}
		this.id = id;
		this.name = name;
		this.role = role;
		this.status = status;
	}

	public LombokNonNullDemo4(@NonNull final Long id, final Integer role, @NonNull final Boolean status) {
		if (id == null) {
			throw new NullPointerException("id is marked non-null but is null");
		}
		if (status == null) {
			throw new NullPointerException("status is marked non-null but is null");
		}
		this.id = id;
		this.role = role;
		this.status = status;
	}
}

5. Conclusion

We have covered how to work with Lombok @NonNull annotation with several examples. You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.

You can checkout source code from our github repository.

You might be interested in our other following Lombok Tutorials :

  1. Lombok @Getter @Setter and lazy getters
  2. Lombok @ToString
  3. Lombok @EqualsAndHashCode
  4. Lombok @AllArgsConstructor
  5. Lombok @NoArgsConstructor
  6. Lombok @RequiredArgsConstructor
  7. Lombok Data
  8. Lombok @Value
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories