问题描述:
开发中有时遇到java的switch报空指针异常。
解决方法:
switch后面的括号里面的参数只能放int类型的值,注意是只能放int类型,但是放byte,short,char类型的也可以,是因为byte,short,shar可以自动提升(自动类型转换)为int。当参数是空null的时候,null转int就报空指针异常。所以在用switch的时候参数要判断一下时是否为空
public String getStatusStr(String status) {
String strName = "";
if(status!=null){switch(status){case 0:strName = "待处理";break;case 1:strName = "已处理";break;case 2:strName = "已完成";break;} }return strName; }