当switch括号内的变量类型为String并且此变量为外部参数时,必须先进行null判断。Switch(null) NullPointerException

2023-05-16

【强制】当switch括号内的变量类型为String并且此变量为外部参数时,必须先进行null判断。

/**
 * @Author: 陈艺博
 * @Date: 2020-04-01 15:27
 * @Description:
 */
public class Java {
    public static void main(String[] args) {
        method(null);
    }

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

在进入Switch(param)是报 空指针异常

Exception in thread "main" java.lang.NullPointerException
	at words.java.Java.method(Java.java:25)
	at words.java.Java.main(Java.java:21)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当switch括号内的变量类型为String并且此变量为外部参数时,必须先进行null判断。Switch(null) NullPointerException 的相关文章

随机推荐