Avatar ·

Which @NotNull annotation in Java is better to use and why?

Hi everyone! I'm a beginner Java developer and I'm writing my first serious project. I've run into the problem that my methods keep throwing NullPointerException because I forget to check arguments for null. I decided to use @NotNull annotations so that the IDE would warn me about potential issues, but then confusion arose.\n\nIn different projects I see different annotations:\n- `javax.annotation.Nonnull` (from JSR 305)\n- `org.jetbrains.annotations.NotNull` (from IntelliJ IDEA)\n- `lombok.NonNull` (from Lombok)\n- `jakarta.annotation.Nonnull` (from Jakarta EE)\n- `javax.validation.constraints.NotNull` (from Bean Validation)\n\nI don't understand which one to choose for my project. For example, I'm writing code like this:\n\n```java\npublic class UserService {\n public User createUser(String name, String email) {