1

I have a class which is annotated with ParametersAreNonnullByDefault javax annotation. But its constructor and methods accept null parameters and don't throw NullPointerException.

@ParametersAreNonnullByDefault
class MyClass {
    private Integer intField;

    public MyClass(Integer intField) {
        this.intField = intField;
    }

    public Integer someMethod(Integer a) {
        return a;
    }
}

The following lines of code do not throw NullPointerException as I would expect:

MyClass obj = new MyClass(null);
obj.someMethod(null);

As per the documentation, "This annotation can be applied to a package, class or method to indicate that the method parameters in that element are nonnull by default". Where/when is the null check being made for method parameters with this annotation?

krackoder
  • 2,841
  • 7
  • 42
  • 51
  • Possible duplicate of [How do I get @ParametersAreNonnullByDefault to work?](https://stackoverflow.com/questions/5516157/how-do-i-get-parametersarenonnullbydefault-to-work) – dimplex May 31 '18 at 05:45
  • [This](https://www.jetbrains.com/help/idea/parametersarenonnullbydefault-annotation.html) may help too. – dimplex May 31 '18 at 05:56
  • Lombok's @NonNull annotation is much more useful as it implements the null check in the code. It is a more concrete implementation showing what 'must' be done and not just what 'should' be done. – krackoder Jun 01 '18 at 05:48

0 Answers0