javanetbeansnull-checknetbeans-14

How to disable "unnecessary test for null" warning in NetBeans 14?


Short Version

How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE?

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

This is demonstrably false

The @NotNull annotation is only an IDE hint, not a runtime guarantee.

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

  1. I tried clicking the lightbulb, to hopefully configure the warning:

enter image description here

but it only offers to configure null deference warnings - which i definitely want to keep.

  1. I tried pressing Alt+Enter to bring up more options:

enter image description here

but nothing of value appears:

  1. I tried to let it bring me to the area to configure the Null dereferncing hint:

enter image description here

but it definitely has nothing to do with *unnecessary test for null.

  1. I tried searching for a hint or warning named "null":

enter image description here

but it's not there.

  1. I tried searching for a hint or warning named "unnecessary":

enter image description here

but it's not there.

  1. I tried searching for a hint or warning named "test":

enter image description here

but it's not there.

How to turn it off

Which brings me to my question:

Bonus Reading


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 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.