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 243371

Summary: Incorrect warning "Unnecessary test for null - a NullPointerException would already be thrown"
Product: java Reporter: phildoble <phildoble>
Component: HintsAssignee: 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
Product Version = NetBeans IDE 7.4 (Build 201310111528)
Operating System = Mac OS X version 10.9.2 running on x86_64
Java; VM; Vendor = 1.7.0_25
Runtime = Java HotSpot(TM) 64-Bit Server VM 23.25-b01
Comment 1 phildoble 2014-03-29 15:33:21 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;
      }
   }
Comment 2 Ondrej Vrabec 2014-04-18 12:19:16 UTC
phildoble:
wouldn't a NPE be thrown already 4 lines above at:
> this.storageAreaId = storageArea.id;
???
Comment 3 Jiri Prox 2014-05-22 13:57:24 UTC
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
Comment 4 Svata Dedic 2014-05-22 14:14:47 UTC
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.