一、问题提出
1.1 空指针异常
public static void main(String[] args) {
Integer switchCode = null;
System.out.println(switchCode.equals(1));//空指针异常
}
二、使用enum优雅处理
2.1避免空指针异常
public enum SwitchEnum {
CLOSE(0, "关闭"),
OPEN(1, "开启");
private Integer code;
private String name;
SwitchEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Integer switchCode = null;
System.out.println(SwitchEnum.OPEN.getCode().equals(switchCode));
}
}