JDK-6775385 : NPE caused by ConditionalExpression and auto-boxing failure
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-11-24
  • Updated: 2011-02-16
  • Resolved: 2008-11-26
Description
FULL PRODUCT VERSION :
C:\Programme\Java\jdk1.6.0_03\bin>java -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode)
Java SE 6.0 Update 10

ADDITIONAL OS VERSION INFORMATION :
Windows XP SR-2

EXTRA RELEVANT SYSTEM CONFIGURATION :
Testcase run by NetBeans 6.5 IDE


A DESCRIPTION OF THE PROBLEM :
ConditionalExpression assumes c2bExistents.get(c) as primitive byte type instead of class type Byte.

See source code...


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run test case:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/trunk/test/sun/nio/cs/MapCalculator.java?rev=489&view=markup


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
no exception should be thrown here

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Testcase: process[0](sun.nio.cs.MapCalculator):        Caused an ERROR
null
java.lang.NullPointerException
        at sun.nio.cs.MapCalculator.getMultiples(MapCalculator.java:507)
        at sun.nio.cs.MapCalculator.printHeader(MapCalculator.java:710)
        at sun.nio.cs.MapCalculator.process(MapCalculator.java:415)


Test sun.nio.cs.MapCalculator FAILED


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
                Map<Character,Byte> c2bExistents = new HashMap();
                char c = '\u20ac';
                char directEnd = '\u0080';
                Byte exsistent = c < directEnd ? (byte)(c) : c2bExistents.get(c);


Checkout complete sources from:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/?rev=489

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

CUSTOMER SUBMITTED WORKAROUND :
                Map<Character,Byte> c2bExistents = new HashMap();
                char c = '\u20ac';
                char directEnd = '\u0080';
                Byte exsistent;
                if (c < directEnd)
                    exsistent = (byte)(c);
                else
                    exsistent = c2bExistents.get(c);
or:
                Byte exsistent = c < directEnd ? (Byte)(byte)(c) : c2bExistents.get(c);

Comments
EVALUATION ?: is to unbox the Byte result of c2bExistents.get(ch) rather than to box the (byte)c. Unboxing a null Byte may return an NPE. The conclusion:-) "The behavior is correct according to JLS3 section 15.25" -- Jon
26-11-2008