[
https://issues.apache.org/jira/browse/NETBEANS-407?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16374352#comment-16374352
]
Eirik Bakke commented on NETBEANS-407:
--------------------------------------
> Your code doesn't actually check if k might be null or not.
That's correct. But the general existing policy of the null checking warning is
to only warn if there some positive indication that k may be null, e.g. a
"@Nullable" annotation on the return type on getFoo(), or where the compiler
can see that k might be null due to an earlier assignment. The checker should
not (and currently does not, except for this bug) give a warning simply because
every method _might_ return null.
> If you add the if and the warning goes away, then everything should be fine,
> since instanceof also implies != null.
Sorry, I was a little unclear here. The warning goes away even if the method
invocation on k is outside the if statement. As in the following variation of
the code:
{code:java}
public class TestClass {
private Number getFoo() {
return 3;
}
public void testFoo() {
Number k = getFoo();
if (k instanceof Integer) {
System.out.println("It's an Integer (and thus not null)");
}
// No warning here now.
System.out.println(k.doubleValue());
}
}
{code}
> "Dereferencing possible null pointer" after doing instanceof on variable
> (false positive)
> -----------------------------------------------------------------------------------------
>
> Key: NETBEANS-407
> URL: https://issues.apache.org/jira/browse/NETBEANS-407
> Project: NetBeans
> Issue Type: Bug
> Components: java - FindBugs, java - Hints
> Affects Versions: 8.2, 9.0
> Reporter: Eirik Bakke
> Priority: Minor
>
> In the following example, the "boolean foo = k instanceof Integer" assignment
> causes a spurious "Dereferencing possible null pointer" warning to occur on
> the "k.doubleValue()" expression. I believe this did not happen in earlier
> NetBeans versions (pre-8.2). Note that if the assignment to "boolean foo" is
> replaced by an if statement ("if (k instanceof Integer)"), the warning goes
> away.
> {code:java}
> public class TestClass {
> private Number getFoo() {
> return 3;
> }
> public void testFoo() {
> Number k = getFoo();
> // The warning only occurs if this assignment is present.
> boolean foo = k instanceof Integer;
> // Warning on .doubleValue() here: "Dereferencing possible null pointer"
> System.out.println(k.doubleValue());
> }
> }
> {code}
> This bug was previously described at
> https://netbeans.org/bugzilla/show_bug.cgi?id=269324 .
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists