🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we’ll explore how you can utilize Lombok @NonNull annotation to make your code more resilient against null-related issues.
What is @NonNull?
The @NonNull annotation in Lombok is designed to help you automatically generate null checks for your variables. By annotating a variable or a method parameter with @NonNull, Lombok will insert a null check at the beginning of the method or constructor, and if the check fails, a NullPointerException will be thrown.
Without Project Lombok
Without Lombok @NonNull annotation, we need to manually write a code to handle null pointer exceptions:package net.javaguides.lombok.nonnull;
import net.javaguides.lombok.User;
public class NonNullExample {
private String name;
public NonNullExample(User user) {
if (user == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = user.getFirstName();
}
public static void main(String[] args) {
NonNullExample example = new NonNullExample(null);
}
}
With Project Lombok
With Project Lombok, we are not writing any code to handle null pointer null but we are just adding @NonNull annotation:package net.javaguides.lombok.nonnull; import lombok.NonNull; import net.javaguides.lombok.User; public class NonNullLombokExample { private String name; public NonNullLombokExample(@NonNull User person) { this.name = person.getFirstName(); } public static void main(String[] args) { NonNullLombokExample example = new NonNullLombokExample(null); } }The @NonNull annotation automatically generates that null check for you.
Common Use Cases
Method Parameters: As seen above, you can use @NonNull to annotate method or constructor parameters to ensure that they are never passed a null value.
Field Initialization: You can use @NonNull on fields that are expected to be initialized immediately, either directly or through a constructor.
Limitations and Considerations
While @NonNull is beneficial, there are certain things to keep in mind:
Lombok doesn't add null-checks everywhere: and Lombok adds null-checks in constructors, setters, and builder methods. It won't add checks to general methods or in other places.
Runtime Overhead: Remember that the checks Lombok adds are runtime checks. They will slightly increase the execution time whenever they're triggered. However, this overhead is usually negligible.
Debugging: If you're not familiar with Lombok, it might initially be confusing when you see a NullPointerException thrown from a line that doesn't contain an explicit null check. It's essential to remember that Lombok has added these checks during the compilation process.
Reference
GitHub Repository
You can view the source code of this article on my GitHub repository at https://github.com/RameshMF/project-lombok-tutorialMy Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment