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.
The following code snippet results in an incorrect "Unnecessary test for null" hint: List<Integer> maxStepS = em.createQuery("SELECT MAX(wfs.id.stepId) FROM WorkflowStepEntity wfs WHERE wfs.id.offerId = :offerId", Integer.class) .setParameter("offerId", offerId) .getResultList(); Integer maxStepId = maxStepS.isEmpty() ? 0 : maxStepS.get(0); return maxStepId == null ? 0 : maxStepId; The element [0] of the list contains "null", so it must be checked before returning the result as an "int".
This is funny. Actually there's one NPE warning *missing*. The expression maxStepS.isEmpty() ? 0 : maxStepS.get(0) is evaluated, contrary to the (supposed) expectation so that the result is int (primitive) and unboxing is attempted on maxStepS.get(0). According to JLS 15.25. the expression is "numeric conditional expression", because 2nd operand is a numeric constant and 3rd operand is convertibel (JLS 5.1.8., Unboxing conversion) to int type. So the actual result is an unboxed "int" primitive type, which is then boxed again into maxSteps, which is *never null* provided that the statement actually completes normally. So in your case, the "maxStepId == null" is indeed redundant. I will try to add a potentiall null warning for the unboxing conversion.
Then please try this class - running results in a NPE: public class NullTest { public static void main(String... args) { new NullTest().test(); } public NullTest() { } private void test() { System.out.println(getInteger(null)); System.out.println(getInteger(0)); } private Integer getInteger(Integer value) { List<Integer> maxStepS = Arrays.asList(new Integer[]{value}); Integer maxStepId = maxStepS.isEmpty() ? 0 : maxStepS.get(0); return maxStepId == null ? 0 : maxStepId; } private Integer getValue(Integer value) { return value; } } Probably this is probably a problem with the JDK? Version is: openjdk version "1.8.0_112" OpenJDK Runtime Environment (build 1.8.0_112-b15) OpenJDK 64-Bit Server VM (build 25.112-b15, mixed mode)
I know it does, I've tried before :) But it fails on line Integer maxStepId = maxStepS.isEmpty() ? 0 : maxStepS.get(0); during unboxing of maxStepS.get(0) result.
Ah, thank You - now I see. That behaviour was really unexpected. So the warning about unnecessary unboxing is wrong (for "new Integer(0)" in this line instead of just "0"), as it is necessary to avoid conversion to int type resulting in NPE.
ah, good point. I'll add that special case.
Fixed unnecessary unboxing + npe check in jet-main#24aac4c19728
Integrated into 'main-silver', will be available in build *201612180001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-silver/rev/24aac4c19728 User: Svata Dedic <sdedic@netbeans.org> Log: #269269: improved NPE an unboxing checks for primitive wrappers and conditional expressions