1

With following method,

public void setDescription(final String description) {

    this.description = description;

    if (this.description == null || this.description.isEmpty()) {
        this.description = "[description]";
    }
}

NetBeans shows a hint on this.description == null part saying

Unnecessary test for null - the expression is never null

What does this mean? What am I missing?


UPDATE

I have another situation with the same (kind of) hint.

class User {

    public void setUsername(final String username) {
        if (this.username != null) { // <-- right here!
            throw new IllegalStateException(
                "already has a username");
        }
        this.username = username;
    }

    @NotNull
    private String username;
}

NetBeans shows a hint with the same text for the part this.username != null. In this case NetBeans oversees @NotNull. Track following issue if you're interested.

https://netbeans.org/bugzilla/show_bug.cgi?id=262707

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • NetBeans shows this message, because it means that the string is never `null` and you can remove this check. But I can not say why. – Jens Feb 09 '15 at 07:19
  • Show us how you are calling `setDescription()` – TheLostMind Feb 09 '15 at 07:22
  • 5
    I would say it's a bug and should be ignored. Unless the method is private, there is no way Netbeans can know it has seen all the call sites, and therefore no way it can know that the parameter is never null. OTOH it's a very strange pice of code. You've changed an unambiguous `null` to an ambiguous `"[description]"`. – user207421 Feb 09 '15 at 07:23
  • Have you tried to clean/rebuild your code. This is indeed strange. – Matt Feb 09 '15 at 07:31
  • @Jin Kwon Can you check if my answer is correct? – Ortomala Lokni Mar 23 '15 at 12:14
  • Even if all the callers were known currently to pass a value that can never be null, the IDE cannot know the future, when a nullable value *is* passed. – Jeff Holt Jul 14 '22 at 18:51

1 Answers1

2

The behavior you describe is associated with the Bug 226923 - Wrong warning 'the expression is never null' .

The bug should be patched until netbeans 7.3.1. I test your code in netbeans 8.0.2 and no hint appears.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240