JDK-6360739 : Tertiary operator throws NPE due to reduntant casting
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2005-12-08
  • Updated: 2010-04-02
  • Resolved: 2005-12-13
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Redundant casting of null makes tertiary operator throw NPE where it wouldn't otherwise (without casting).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run sample Bug class attached.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Tertiary operator should return null
ACTUAL -
Tertiary operator throws NullPointerException

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NullPointerException
        at Bug.main(Bug.java:12)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Date;

public class Bug {

    public static void main(String[] args) {
        Date date = null;

        // works fine
        Long epoch1 = (date == null) ? null : date.getTime() / 1000L;

        // throws NullPointerException
        Long epoch2 = (date == null) ? (Long) null : date.getTime() / 1000L;
    }

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Remove redundant casting