JDK-7103921 : NPE in javac for strings-in-switch
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2011-10-22
  • Updated: 2015-08-19
  • Resolved: 2011-10-24
Related Reports
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 7 Ultimate  6.1.7601 Service Pack 1 Build 7601 x64

A DESCRIPTION OF THE PROBLEM :
I seem to get an error when I try to use the nextLine method from an object of the Scanner class as the parameter for another method.


ERROR MESSAGES/STACK TRACES THAT OCCUR :
--------------------Configuration: Solvate Box - JDK version 1.7.0 <Default> - <Default>--------------------
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.comp.Lower.visitWhileLoop(Lower.java:3323)
	at com.sun.tools.javac.tree.JCTree$JCWhileLoop.accept(JCTree.java:837)
	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)

Process completed.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.util.Scanner;

public class Solvate_Box {
   Scanner reader = new Scanner(System.in);
    public static void main(String[] args) {
		MainMenu.menuAction(reader.nextLine());
	}
}

class MainMenu {

	public void displayMenu(){
		System.out.println("This program is used to read input and and generate output for a protein data bank file (.pdb) ");
	    System.out.println("For more information about pdb files, refer to the help prompt");
	}

	public void menuAction(String input){
		while (0==0){

			switch (input){
			case ("h"):
			case ("H"): //HelpMe.prompt(); break;
			default: System.out.println("Please input a valid option"); break;
			}
		}

	}
}
---------- END SOURCE ----------

Comments
EVALUATION This issue is a duplicate of 7071246, which will be fixed in 7u2. The crash is caused by the extra "()" the string switch label as in case ("h"): As a workaround, remove the "()" to give case "h":
24-10-2011