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 247409 - False positive hints due to assert : "Dereferencing possible null pointer", "Unnecessary test for null"
Summary: False positive hints due to assert : "Dereferencing possible null pointer", "...
Status: RESOLVED DUPLICATE of bug 249320
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.1
Hardware: PC Windows XP
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-23 15:29 UTC by nimarukan
Modified: 2015-11-13 09:34 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 nimarukan 2014-09-23 15:29:21 UTC
This example gives two incorrect "Dereferencing possible null pointer" hints.
1. The detector may be interpreting the assert condition incorrectly.
2. The detector should not assume the assert always runs.  (asserts only run if assertions are enabled.)

public class DerefNullHintBad {
  public static void run(Object value) {
    assert value == null || "".equals(value) : value.toString();
    if (value != null) {
      // ...
    }
  }
  public static void main(String[] ignore) {
    run(null);
    run("");
    System.err.println("nothing thrown");
  }
}

1. On the 'assert' line: "Dereferencing possible null pointer".  

But the assert condition explicitly allows null, so the value.toString() should only run if value is not null.  

2. On the 'if' line: "Unnecessary test for null - a NullPointerException would already be thrown"

This is wrong for two reasons: 
- same as #1 above, the assert does not throw NullPointerException, and 
- assertions only run if assertions are enabled.
Comment 1 Svata Dedic 2014-12-12 12:19:45 UTC

*** This bug has been marked as a duplicate of bug 249320 ***
Comment 2 Svata Dedic 2015-11-13 09:34:33 UTC
Seems to work in the new implementation. Wait for the umbrella issue to close.

Sadly the situation when a `if' repeats asserted condition and is NOT reported as redundant is hard to achieve (assert p != null; ... if (p != null) { ... }). In your case (since the exact assertion conditions permit both value == null and non-null (""), the warning won't be there, but in general case - yes.