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.
Summary: | Incorrect warning "Unnecessary test for null - a NullPointerException would already be thrown" | ||
---|---|---|---|
Product: | java | Reporter: | phildoble <phildoble> |
Component: | Hints | Assignee: | Svata Dedic <sdedic> |
Status: | RESOLVED WORKSFORME | ||
Severity: | normal | ||
Priority: | P3 | ||
Version: | 7.4 | ||
Hardware: | Macintosh | ||
OS: | Mac OS X | ||
Issue Type: | DEFECT | Exception Reporter: |
Description
phildoble
2014-03-29 15:28:57 UTC
In the following code, the constructor is at times correctly called with its storageArea argument as null, but the IDE issues a warning on the if (storageArea != null) { line... /** * Constructor. * @param id the id of this in the database; <code>null</code> for new unpersisted instances. * @param site The site at which this location exists. * @param storageArea The storage area within <code>site</code> at which this location exists; <code>null</code> if not a storage area. * @param rackSlot The racking bay and tier, if the location is racking in a storage area. * @param title a title for this; if <code>null</code> or empty after trimming, a suitable value is generated by this constructor. */ protected Location(Short id, Site site, StorageArea storageArea, RackSlot rackSlot, String title) { this.id = id; this.siteId = site.id; this.storageAreaId = storageArea.id; this.rackSlot = rackSlot; if (title == null || (title = title.trim()).isEmpty()) { StringBuilder sb = new StringBuilder(); if (storageArea != null) { sb.append(storageArea); } else { sb.append(site); } if (rackSlot != null) { sb.append(" ").append(rackSlot); } this.title = sb.toString(); } else { this.title = title; } } phildoble:
wouldn't a NPE be thrown already 4 lines above at:
> this.storageAreaId = storageArea.id;
???
The hint is correct. If storageArea parameter is null, the NullPointerException will be thrown at line this.storageAreaId = storageArea.id; so the test if (storageArea != null) ... is not necessary Possibly less confusing would be to display hint 'Deferencing possible null value' for line 4 This would however require substantial changes in the detection algorithm as the value ability to contain null (null check expression) is discovered later than the statements potentially in error. |