Created
January 8, 2021 22:45
-
-
Save nicolasff/8058ffc3f312e706cc7f98d892200c80 to your computer and use it in GitHub Desktop.
NPE on switch(null)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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