Backend Development 7 min read

Understanding NullPointerException in Java String Switch Statements

The article explains why a NullPointerException occurs when a null String is used in a switch statement, detailing the underlying equals() and hashCode() calls, the required preconditions, and how Java compiles String switches, supplemented with illustrative code examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding NullPointerException in Java String Switch Statements

NullPointerException (NPE) is one of the most frequently encountered exceptions in Java development. Good coding standards can avoid many of these issues; the Alibaba coding manual even specifies that when using a switch statement, the variable inside the parentheses must be of type String and must be an external parameter.

Variable Type is String

Consider the following code and ask what it will print:

public class SwitchString {

    public static void main(String[] args) {
        method(null);
    }

    public static void method(String param) {
        switch (param) {
            // will never enter here
            case "sth":
                System.out.println("it's sth");
                break;
            // will never enter here
            case "null":
                System.out.println("it's null");
                break;
            // will never enter here
            default:
                System.out.println("default");
        }
    }
}

If we run this program, it throws a NullPointerException . The common misconception is that the exception occurs because param is null on line 8, but the real reason lies in how a String switch is implemented.

An NPE is thrown when a method or field is accessed on a null reference. In a String switch , the compiler translates the statement into calls to hashCode() and equals() . Since param is null, invoking these methods triggers the exception.

Before JDK 7, switch only supported primitive types. Starting with JDK 7, it also supports String . The compiler actually converts the switch into a hash‑code comparison followed by an equals() check.

Decompiling the above class yields:

public class SwitchString {

    public static void main(String[] args) {
        method((String) null);
    }

    public static void method(String param) {
        byte var2 = -1;
        switch (param.hashCode()) {
            case 114215:
                if (param.equals("sth")) {
                    var2 = 0;
                }
                break;
            case 3392903:
                if (param.equals("null")) {
                    var2 = 1;
                }
                break;
        }
        switch (var2) {
            case 0:
                System.out.println("it's sth");
                break;
            case 1:
                System.out.println("it's null");
                break;
            default:
                System.out.println("default");
        }
    }
}

The generated bytecode shows that the original switch on a String is performed by first computing hashCode() and then using equals() to resolve collisions. Because param is null, both method calls fail, resulting in the NPE.

External Parameter Condition

If we modify the code to switch directly on a literal null :

public class SwitchString {

    public static void main(String[] args) {
        switch (null) {
            // will never enter here
            case "sth":
                System.out.println("it's sth");
                break;
            // will never enter here
            case "null":
                System.out.println("it's null");
                break;
            // will never enter here
            default:
                System.out.println("default");
        }
    }
}

This code does not compile, because the compiler can detect that a null literal cannot be used as a String switch argument.

Summary

The variable inside a switch must be of type String .

The variable must be an external (non‑null) parameter; otherwise the implicit calls to equals() and hashCode() will cause a NullPointerException .

If a null literal is used directly in the switch expression, the code fails to compile.

javaBackend Developmentexception handlingnullpointerexceptionString Switch
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.