3

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

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

2 Answers2

1

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

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • Unfortunately `@SuppressWarnings("null")` suppresses *`Dereference null pointer`* warning: **which**, as i said in the original question, **i definitely want to keep**. The question is how to disable *"unnecessary test for null"* (and not disable anything else) – Ian Boyd Jul 14 '22 at 17:34
  • OK - in that case you should clarify your precise question because the title, the opening sentence and the closing sentence are all only asking how to disable/turn off the _"unnecessary test for null"_ warning, and my answer explained how to do that. What you seem to want is to turn off the compiler warning, yet still retain the associated NetBeans hint, and I suspect that can't be done. Regardless, your question needs clarification. – skomisa Jul 14 '22 at 18:33
  • It isn't a compiler warning; there is no compiler warning emitted by Java, or the Java compiler, saying "unnecessary test for null". Java does not do that. Java does not check for *unnecessary check for null*. That is why it is not a Java question. It is a **NetBeans** question. That is why it was tagged as a **NetBeans** question: hoping to solve this **NetBeans** issue with **NetBeans** giving an incorrect warning in **NetBeans**. – Ian Boyd Jul 14 '22 at 18:36
  • OK. Regardless, you still need to clarify your question, and while it's tough to prove a negative, the evidence seems pretty clear that what you seem to want can't be done in NetBeans. – skomisa Jul 14 '22 at 18:39
  • I've edited the question to clarify that i want to disable the *"unnecessary test for null"* warning in NetBeans 14 IDE, and i put it in big bold writing that i **do not want to disable null reference checking**. – Ian Boyd Jul 14 '22 at 18:53
  • [1] OK, but your title, opening sentence and closing sentence are still just asking how to disable "unnecessary test for null" and nothing more, so it remains unclear. [2] It seems implausible to claim that the _"Null dereferencing hint...definitely has nothing to do with *unnecessary test for null"_, given that the @SuppressWarnings("null") annotation suppresses both messages, or am I misunderstanding your point? (To be clear, I am not referring to what _should_ happen, but to what _does_ happen.) – skomisa Jul 14 '22 at 19:21
  • I want to disable *unnecessary test for null* warning. I do **not** want to disable *dereference null pointer* warning. – Ian Boyd Jul 14 '22 at 20:11
0

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.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219