📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
What is @NonNull?
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.
Comments
Post a Comment
Leave Comment