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 269269 - Unnecessary test for null
Summary: Unnecessary test for null
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.2
Hardware: PC Linux
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-12-08 10:41 UTC by Peter Nabbefeld
Modified: 2016-12-18 02:41 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 Peter Nabbefeld 2016-12-08 10:41:11 UTC
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".
Comment 1 Svata Dedic 2016-12-14 15:08:18 UTC
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.
Comment 2 Peter Nabbefeld 2016-12-14 17:16:31 UTC
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)
Comment 3 Svata Dedic 2016-12-14 18:53:46 UTC
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.
Comment 4 Peter Nabbefeld 2016-12-14 19:40:49 UTC
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.
Comment 5 Svata Dedic 2016-12-14 19:46:18 UTC
ah, good point. 
I'll add that special case.
Comment 6 Svata Dedic 2016-12-15 12:02:43 UTC
Fixed unnecessary unboxing + npe check in jet-main#24aac4c19728
Comment 7 Quality Engineering 2016-12-18 02:41:03 UTC
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