Issue
Short Version
How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE?
src="https://i.stack.imgur.com/Hw9Tf.png" alt="enter image description here" />
Long Version
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
- because the
o
parameter was tagged as @NotNullo
- it must be impossible for
o
to benull
- so it must be that the
if
statement is unnecessary
This is demonstrably false
The @NotNull annotation is only an IDE hint, not a runtime guarantee.
- just because an argument to a method is tagged as @NotNullable
- does not mean it cannot be
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.
Research Effort
- I tried clicking the lightbulb, to hopefully configure the warning:
but it only offers to configure null deference warnings - which i definitely want to keep.
- I tried pressing Alt+Enter to bring up more options:
but nothing of value appears:
- I tried to let it bring me to the area to configure the Null dereferncing hint:
but it definitely has nothing to do with *unnecessary test for null.
- I tried searching for a hint or warning named "null":
but it's not there.
- I tried searching for a hint or warning named "unnecessary":
but it's not there.
- I tried searching for a hint or warning named "test":
but it's not there.
How to turn it off
Which brings me to my question:
- given that NetBeans IDE 14 has no way to turn off "unnecessary test for null" warning
- how do i turn off the "unnecessary test for null" warning in NetBeans IDE 14?
Bonus Reading
- What does unnecessary test for null mean?
- Netbeans tells me to remove null comparison, but that breaks my code ("First thing to note is: NetBeans is wrong")
- BugZilla: Bug 226923 - Wrong warning 'the expression is never null'
- Bugzilla: Bug 262707 - Add configuration of Nullable/NonNull annotations. Use FQNs or allow to exclude FQNs.
Solution
The answer is: it cannot be done.
NetBeans provides no way to disable the unnecessary test for null warning.
Workaround
As other people in other answers have noted:
- the value can be null
- NetBeans is wrong thinking it cannot be null
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.
Answered By - Ian Boyd
Answer Checked By - Terry (JavaFixing Volunteer)