This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 244424 - Inappropriate "dereferencing possible null pointer" warning
Summary: Inappropriate "dereferencing possible null pointer" warning
Status: RESOLVED DUPLICATE of bug 226083
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: PC Linux
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-11 12:50 UTC by pepijn
Modified: 2014-06-06 14:59 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description pepijn 2014-05-11 12:50:02 UTC
Product Version = NetBeans IDE 8.0 (Build 201403101706)
Operating System = Linux version 3.11.0-031100-generic running on amd64
Java; VM; Vendor = 1.7.0_55
Runtime = Java HotSpot(TM) 64-Bit Server VM 24.55-b03

Reference the following code:

public class NullPointerTest {
    public void test(String arg1, String arg2) {
        boolean useArg1 = arg1 != null;
        if (useArg1) {
            System.out.println(arg1.length()); // Inappropriate "dereferencing possible null pointer" warning
        } else {
            System.out.println(arg2.length()); // *No* "dereferencing possible null pointer" warning
        }
    }
}

The IDE gives a warning on the first length() invocation, saying "dereferencing possible null pointer". This is wrong, as that branch is guarded by a variable which is only true when arg1 is *not* null! The IDE should be able to infer from its flow analysis that arg1 can never be null at that location.

And for the second invocation of length(), where the operand *can* be null, it does not give the warning. I assume the warning for arg1 is triggered by the explicit null check on it? If so then it's ironic and not very useful that the actual result is exactly opposite from the intended result...

I'm not sure this is a bug. It may just be that Netbeans' flow analysis is not sophisticated enough to detect that arg1 can never be null, but in that case I propose that this hint be turned off by default. I know from my own experience that this construct is relatively common, and right now Netbeans is littering my code with this incorrect warning.
Comment 1 Svata Dedic 2014-06-06 14:59:36 UTC
Understood, but requires a different approach to the data flow analysis. See enhancement #226083

*** This bug has been marked as a duplicate of bug 226083 ***