JDK-8143186 : I get NullPointerException when I use long value and null in ternary operator
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 8u51
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-11-16
  • Updated: 2025-06-20
  • Resolved: 2015-11-23
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
C:\Users\Tim>java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 10 x64

A DESCRIPTION OF THE PROBLEM :
Try running this:

public class Main {

	public static void main(String[] args) {
	      Long var = null;
	      System.out.println("text" == null ? 0L : var);
	}
}

I only get NullPointerException when i use a Number value.
To fix this, I need to cast the long value like this:
    Long var = null;
    System.out.println("text" == null ? (Long)0L : var);




REPRODUCIBILITY :
This bug can be reproduced always.


Comments
This behaviour seems to be in line with the JLS spec. See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25
23-11-2015

This doesn't look like an issue. When the first value is a primitive long, the second value has to be unboxed to get a primitive long value. The second value is a null - so unboxing results in NPE. When first value is cast to Long, both the results are now Objects and there is no need for unboxing and it works fine. Moving across to dev-team for opinion.
18-11-2015