如何避免空指针?

1、字符串比较,常量放前面

if(status.equals(SUCCESS)){
    
}

2、初始化默认值

1 User user = new User();
2 String name = StringUtils.EMPTY;

3、返回空集合

public List getUserList(){
    List list = userMapper.gerUserList();
    return list == null ? new ArrayList() : list;
}

4、Optional

java8新特性  JDK8新特性之Optional

posted @ 2019-11-18 11:08  王岳阳  阅读(265)  评论(0编辑  收藏  举报