JDK-7105317 : NPE at com.sun.tools.javac.comp.Lower.visitStringSwitch(Lower.java:3456)
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_10
  • CPU: sparc
  • Submitted: 2011-10-26
  • Updated: 2012-03-20
  • Resolved: 2011-10-28
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
$ /usr/jdk/instances/jdk1.7.0/bin/java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b148)
Java HotSpot(TM) Server VM (build 21.0-b18, mixed mode)
$ /usr/jdk/instances/jdk1.7.0/bin/java -d64 -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b148)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b18, mixed mode)
$ /usr/jdk/instances/jdk1.7.0/bin/javac -version
javac 1.7.0
$ /usr/jdk/instances/jdk1.7.0/bin/javac -d64 -version
javac 1.7.0

ADDITIONAL OS VERSION INFORMATION :
$ more /etc/release
                           Oracle Solaris 11 11/11 X86
  Copyright (c) 1983, 2011, Oracle and/or its affiliates.  All rights reserved.
                            Assembled 11 October 2011
$ uname -a
SunOS perf2270-59 5.11 11.0 i86pc i386 i86pc




A DESCRIPTION OF THE PROBLEM :
Use of  the below code in Switc-String results in javac failure:

                case ("a"):

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
$ cat >SwitchStringBug.java <EOF
public class SwitchStringBug extends Thread {
public static void main(String[] args) {
        for (String arg : args) {
                switch (arg) {
                case ("a"):
                        System.err.println("A");
                        break;
                }
        }
}
} /** End Class. */
EOF
$ /usr/jdk/instances/jdk1.7.0/bin/javac SwitchStringBug.java

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
javac exit with status 0 and compiled SwitchStringBug.class file.
ACTUAL -
javac exit with status 4 and no compiled SwitchStringBug.class file.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
$ /usr/jdk/instances/jdk1.7.0/bin/javac SwitchStringBug.java
An exception has occurred in the compiler (1.7.0). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.NullPointerException
        at com.sun.tools.javac.comp.Lower.visitStringSwitch(Lower.java:3456)
        at com.sun.tools.javac.comp.Lower.visitSwitch(Lower.java:3357)
        at com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:959)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:70)
        at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:160)
        at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:3311)
        at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:781)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:70)
        at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:160)
        at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:3311)
        at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:781)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.comp.Lower.visitForLoop(Lower.java:3332)
        at com.sun.tools.javac.tree.JCTree$JCForLoop.accept(JCTree.java:872)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.comp.Lower.visitArrayForeachLoop(Lower.java:3196)
        at com.sun.tools.javac.comp.Lower.visitForeachLoop(Lower.java:3135)
        at com.sun.tools.javac.tree.JCTree$JCEnhancedForLoop.accept(JCTree.java:907)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:70)
        at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:160)
        at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:3311)
        at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:781)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.tree.TreeTranslator.visitMethodDef(TreeTranslator.java:144)
        at com.sun.tools.javac.comp.Lower.visitMethodDefInternal(Lower.java:2619)
        at com.sun.tools.javac.comp.Lower.visitMethodDef(Lower.java:2538)
        at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:669)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.comp.Lower.visitClassDef(Lower.java:2283)
        at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:591)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2160)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:2180)
        at com.sun.tools.javac.comp.Lower.translateTopLevelClass(Lower.java:3650)
        at com.sun.tools.javac.main.JavaCompiler.desugar(JavaCompiler.java:1393)
        at com.sun.tools.javac.main.JavaCompiler.desugar(JavaCompiler.java:1271)
        at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:870)
        at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:829)
        at com.sun.tools.javac.main.Main.compile(Main.java:417)
        at com.sun.tools.javac.main.Main.compile(Main.java:331)
        at com.sun.tools.javac.main.Main.compile(Main.java:322)
        at com.sun.tools.javac.Main.compile(Main.java:76)
        at com.sun.tools.javac.Main.main(Main.java:61)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class SwitchStringBug extends Thread {
public static void main(String[] args) {
        for (String arg : args) {
                switch (arg) {
                case ("a"):
                        System.err.println("A");
                        break;
                }
        }
}
} /** End Class. */
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use traditional "if/elseif/else" coding style.

Comments
EVALUATION Duplicate of 7071246 and will be fixed in 7u2; a workaround is to simply not surround the string constant in the case label with redundant parenthesis.
28-10-2011