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

720 Views Asked by At

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

  • because the o parameter was tagged as @NotNullo
  • it must be impossible for o to be null
  • 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

  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:

  • 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

2

There are 2 best solutions below

0
Ian Boyd On BEST ANSWER

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.

7
skomisa On

You can turn off the "Unnecessary test for null" warning using the Java annotation @SuppressWarnings("null"). That annotation is found in java.lang, and there is no need for an import.

The OpenJDK Javadoc for SuppressWarnings for JDK 17 states:

Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element) ... As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.

From the linked documentation to section 9.6.4.5 of the Java Language Specification, @SuppressWarnings appears to do exactly what you want, with my emphasis added:

9.6.4.5. @SuppressWarnings

Java compilers are increasingly capable of issuing helpful "lint-like" warnings. To encourage the use of such warnings, there should be some way to disable a warning in a part of the program when the programmer knows that the warning is inappropriate.

Here's sample code, based on that in the OP:

package suppression;
import javax.validation.constraints.NotNull; // Jakarta EE 8
//import jakarta.validation.constraints.NotNull; // Jakarta EE 9

public class Suppression {

    public static void main(String[] args) {

        Suppression supp = new Suppression();
        Object o = supp.getTestValue();
        supp.doSomething(o);
        supp.doSomething2(o);
    }

    Object getTestValue() {
        Object o = null;
        return o;
    }

    private void doSomething(@NotNull Object o) {
        
        if (o == null) {
            System.out.println("Object is null!");
        }
    }

    @SuppressWarnings("null")
    private void doSomething2(@NotNull Object o) {
        
        if (o == null) {
            System.out.println("Object is null!");
        }
    }
}

Here's a screenshot of that code in NetBeans 14 which shows:

  • The unwanted warning "Unnecessary test for null" is shown on line 22 in method doSomething().
  • The annotation @SuppressWarnings("null") on line 27 prevents the unwanted warning "Unnecessary test for null" being shown on line 30 in the otherwise identical method doSomething2().

SuppressWarnings