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.
Product Version = NetBeans IDE 7.3 (Build 201302132200) Operating System = Windows 7 version 6.1 running on amd64 Java; VM; Vendor = 1.6.0_34 Runtime = Java HotSpot(TM) 64-Bit Server VM 20.9-b04 Within my code I have the following structure: if ( null == a) { return; } for (ClassType c : listofClases) { // placed here as common code block. if (null == a) { // <===== Reported as unnecessary test for null. return; } // 15 separate tests of this nature which could each set a to null. if (c.getId().getValue() > compareAgainst.longValue()) { a = (aList.hasNext()) ? (AType)aList.next() : null; // <========== Incorrect report due to this possible assignment. } } Admittedly, this may not be the cleanest way to accomplish this; however, it works and as we all know, "If it ain't broke. Don't mess with it!" The hint for the null at the beginning of the loop is correct if the code is viewed linearly without the loop. It is impossible to enter the loop with a being null. However, based on the following 15 queries which can result in a being set to null, the hint is incorrectly applied.
Created attachment 132875 [details] IDE log
I have a similar problem. Here is the code for reproducing the bug. ---- begin package netbeans; import java.util.List; public class NullHint { public void foo(List<Object> objs) { Object bar = null; for (Object obj : objs) { System.err.println(obj); if (bar == null) { bar = new Object(); } } // if objs is empty, bar is null here. if (bar != null) { // NB7.3 says this check is unnecessary System.err.println(); } } } ---- end
Another example: // code is completely dummy; just to demo the "Unnecessary test for null" warning private String demoNullHint(Object object, String argument, String defaultValue) { if (object==null && (argument==null || argument.equals("test"))) { return defaultValue; } // warning here: "Unnecessary test for null" if (object!=null) { // if } else { // else } return "some value"; }
I'll push fixes for the first two usecases into the trunk soon. The last usecase is a duplicate of bug #227954.
http://hg.netbeans.org/jet-main/rev/d7c433595615
Integrated into 'main-golden', will be available in build *201304172301* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-golden/rev/d7c433595615 User: Jan Lahoda <jlahoda@netbeans.org> Log: #227745: NPECheck hints must properly handle enhanced for loops.
v.
Backported to release73 branch: http://hg.netbeans.org/releases/rev/58284df11288
Integrated into 'releases', will be available in build *201305042200* or newer. Wait for official and publicly available build. Changeset: http://hg.netbeans.org/releases/rev/58284df11288 User: Jan Lahoda <jlahoda@netbeans.org> Log: #227745: NPECheck hints must properly handle enhanced for loops.
*** Bug 229490 has been marked as a duplicate of this bug. ***