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 268694 - Undue hint about dereferencing possible null pointer
Summary: Undue hint about dereferencing possible null pointer
Status: REOPENED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-26 13:15 UTC by aquaglia
Modified: 2016-10-26 15:53 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Screenshot showing the hint (11.07 KB, image/png)
2016-10-26 13:15 UTC, aquaglia
Details

Note You need to log in before you can comment on or make changes to this bug.
Description aquaglia 2016-10-26 13:15:55 UTC
Created attachment 162648 [details]
Screenshot showing the hint

Product Version: NetBeans IDE 8.1 (Build 201510222201)
Updates: NetBeans IDE is updated to version NetBeans 8.1 Patch 1
Java: 1.8.0_45; Java HotSpot(TM) 64-Bit Server VM 25.45-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_45-b15
System: Windows 7 version 6.1 running on amd64; UTF-8; en_GB (nb)
User directory: C:\Users\quaglan\AppData\Roaming\NetBeans\8.1
Cache directory: C:\Users\quaglan\AppData\Local\NetBeans\Cache\8.1


I stumbled against the following case:

If I write:

    public static boolean equals(List<String> a, List<String> b) {
        if (a == null && b == null) {
            return true;
        }

        if (a == null && b.isEmpty()) {
            return true;
        }

        if (b == null && a.isEmpty()) {
            return true;
        }
...


I get a hint about dereferencing a possible null pointer on the third if, even though it can be statically determined that this cannot happen since the first if already handles the case in which both a and b are null.
Comment 1 Milutin Kristofic 2016-10-26 14:04:10 UTC
You can have 
a = null;
b = is non-empty list.

First if - if (a == null && b == null) will no pass since b is not null

Second if if (a == null && b.isEmpty()) { will no pass since b is not empty

Third if if (b == null && a.isEmpty()) { will throw nullpointer exception.



You can fix this by changing first if to

if (a == null || b == null)
Comment 2 aquaglia 2016-10-26 15:07:09 UTC
Wait a sec:

        if (a == null && b == null) {
            return true;
        }

        if (a == null && b.isEmpty()) {
            return true;
        }

//the expressions are evaluated from left to right and are short circuited
//so, if (b == null) I know a is not null otherwise I would not be here
// if (b = non-empty list) I do not need to evaluate a.isEmpty()

        if (b == null && a.isEmpty()) {
            return true;
        }
Comment 3 Milutin Kristofic 2016-10-26 15:53:57 UTC
Ok, sorry, you are right. Sending to java hints.