Skip to content

Instantly share code, notes, and snippets.

@nicolasff
Created January 8, 2021 22:45
Show Gist options
  • Save nicolasff/8058ffc3f312e706cc7f98d892200c80 to your computer and use it in GitHub Desktop.
Save nicolasff/8058ffc3f312e706cc7f98d892200c80 to your computer and use it in GitHub Desktop.
NPE on switch(null)
public class Test {
enum Choice {
YES,
NO;
}
public static void main(String[] args) {
Choice c = null;
switch (c) {
case YES:
System.out.println("yes");
break;
case NO:
System.out.println("no");
break;
default:
System.out.println("other");
break;
}
}
}
/*
Execution, using Java 11.0.7:
$ javac Test.java
$ java Test
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:10)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment