IntelliJ IDEA complains about null check of @NotNull parameter

I want to use Jetbrains @ Nullable / @ NotNull Annotations in my project.

screenshot

I have a class with the @NotNull field. The constructor, of course, does not accept null , but instead throws an exception. Of course, the parameter of this constructor is also annotated with @NotNull.

Why is IntelliJ IDEA complaining about zero verification? The documentation states:

An element annotated with a NOTNull value of zero is not allowed to return (for methods), go to (parameters) and hold (local variables and fields).

But I still need to check for null values ​​at runtime if the build system does not understand the annotation and accepts an operator like new Car(null) . I am wrong?

+7
java intellij-idea annotations notnull
source share
2 answers

If you use the JetBrains @NotNull annotation, runtime statements will be added to your compiled bytecode to ensure that null will not be passed there. Then, in reality, it makes no sense to write the same checks in the source code. This approach works pretty well for us.

If you use other annotations or simply do not want to use a bytecode, you can turn off this specific warning by pressing Alt + Enter on it, “Edit Validation Settings” and check “Ignore claims”. Such conditional statements are treated as statements using the IDE.

+5
source share

If the parameter for the constructor is not Null, then there is no point to check for a null value using the if statement.

And Since we know that any value held by the Engine parameter is not null, this check will always be false.

Thus, the verification shown in the screenshot will make sense. If you really do not want to see a zero-check check, disable it from the settings section.

In short, the value you are checking is not null inside the constructor, and the IDE knows about it.

+1
source share

All Articles