How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE?
NetBeans as a well-known bug 1 2 3 4 where it will erroneously tell you that a test for null
is unnecessary. For example in the following code:
import javax.validation.constraints.NotNull;
private void doSomething(@NotNull Object o) {
if (o == null) return;
//...do more stuff...
}
The IDE thinks
o
parameter was tagged as @NotNullo
o
to be null
if
statement is unnecessaryThe @NotNull annotation is only an IDE hint, not a runtime guarantee.
null
You can prove this to yourself by passing null
to the doSomething
method. (we can even write the test code so the IDE generates no hints or warnings at all!):
Object o = getTestValue();
doSomething(o);
private Object getTestValue()
{
Object o = null;
return o;
}
private void doSomething(@NotNull Object o) {
Objects.requireNonNull(value);
//...do more stuff...
}
And watch doSomething
method fail - because o
is null
- even though it is tagged @NotNull.
Now, there may be other implementations of @NotNull
, or other compilers that add runtime checks. I'm not talking about those. The NetBeans IDE 14 warning is wrong, so i need to disable it.
but it only offers to configure null deference warnings - which i definitely want to keep.
but nothing of value appears:
but it definitely has nothing to do with *unnecessary test for null.
but it's not there.
but it's not there.
but it's not there.
Which brings me to my question:
The answer is: it cannot be done.
NetBeans provides no way to disable the unnecessary test for null warning.
As other people in other answers have noted:
The correct way to resolve the (incorrect) warning is to obfuscate the check for null.
Rather than calling:
if (customer == null) { ... }
Instead call:
if (Object.isNull(customer)) { ... }
It is the same thing; except this way NetBeans doesn't realize that you're testing the variable for null
, and so doesn't warn you.