前言:
俗话说,不到长城非好汉,那么没遇到空指针 java.lang.NullPointerException 的程序员不是真正的程序员。
目录
3) 获取配置文件的配置信息(如获取阿波罗的配置信息,开发环境配置了配置信息,测试环境没有配置)
1、实体对象为空,然后操作对象会出行空指针异常
1)调用方法获取到的对象为空
DemoEntity entity = getEntity();// entity为null
Boolean bool = entity.getBool();//会有空指针异常
//避免空指针方法
Boolean boo = null != entity ? entity.getBool() : null;
2)接口调用获取到的实体对象为空
DemoEntity entity1 = service.getEntity();// entity1为null
Boolean bool = entity1.getBool();//会有空指针异常
//避免空指针方法
Boolean boo = null != entity1 ? entity1.getBool() : null;
2、包装类型或者字符串等对象为空
1) 包装类型转string
Integer i = getInteger(); //i为null
String str = i.toString();//会有空指针异常
//避免空指针方法
String str = String.valueOf(i)
2) 包装类型==判断
Integer i = getInteger(); //i为null
Boolean boo = i == 2;//会有空指针异常
//避免空指针方法:定义常量值或者枚举值equals比较
Boolean boo = constant.equals(i);//constant 为定义的常量值或者枚举值
3) 获取配置文件的配置信息(如获取阿波罗的配置信息,开发环境配置了配置信息,测试环境没有配置)
@Value("${test.strs}")
public String ymlProperties;
void getProperties() {
//会报空指针异常
if (ymlProperties.equals("test")) {
}
//避免空指针方法,先判空后判断
if (!StringUtils.isEmpty(ymlProperties)) {
if (ymlProperties.equals("test")) {
}
}
}
4) equals 判断
String a = "2";
String b = null;
boolean equals = b.equals(a);//出现空指针异常
//防止空指针方法,将常量放在前面比较
boolean equals = a.equals(b);
3、集合操作空指针异常
1) 集合addAll方法空指针异常
void test() {
List<DemoEntity> list = new ArrayList<>();
list.add(new DemoEntity());
List<DemoEntity> list2 = getList();
list.addAll(list2);// 空指针异常
//避免空指针异常,先判断集合不为空,然后添加集合
if (!CollectionUtils.isEmpty(list2)) {
list.addAll(list2);
}
}
private List<DemoEntity> getList() {
return null;
}
2) 集合过滤、分组空指针异常
List<City> list = new ArrayList<>();
formData(list);//组建测试数据
//过滤获得人口数大于3000的城市,若是人口字段有为null的情况则会出现空指针异常
List<City> collect = list.stream().filter(entity -> entity.getPopulation() > 3000).collect(Collectors.toList());
//避免空指针异常,先过滤掉为空的数据
List<City> collect = list.stream().filter(entity -> null != entity.getPopulation() && entity.getPopulation() > 3000).collect(Collectors.toList());
//根据省份分组,若是省份字段有为null的情况则会出现空指针异常
Map<String, List<City>> provinceMap = list.stream().collect(Collectors.groupingBy(City::getProvince));
//避免空指针异常,先过滤掉为空的数据
Map<String, List<City>> provinceMap = list.stream().filter(f-> null != f.getProvince()).collect(Collectors.groupingBy(City::getProvince));
3) 集合排序空指针异常
List<City> list = new ArrayList<>();
formData(list);//组建测试数据
//若是排序的字段值有空,此排序方式会报空指针异常
list.sort(Comparator.comparing(City::getRank));
//避免空指针异常方法
list.sort(Comparator.comparing(City::getRank,Comparator.nullsFirst(Integer::compareTo)));
4) 集合中的对象为空,操作对象空指针异常
集合中的实体为null,此时集合并不为null,使用 CollectionUtils.isEmpty 并不能判断集合是否为空
List<DemoEntity> list = new ArrayList<>();
DemoEntity demoEntity = getEntity();//demoEntity得到的值为null
DemoEntity demoEntity1 = getEntity();//demoEntity1得到的值为null
list.add(demoEntity);
list.add(demoEntity1);
if (!CollectionUtils.isEmpty(list)) {//list不为空,大小为2
list.forEach(l->{
System.out.println(l.getType());//会有空指针异常
});
}
//避免空指针异常处理
//1、实体为null就不放入list集合,例如
DemoEntity demoEntity2 = getEntity();//demoEntity得到的值为null
if (null != demoEntity2) {
list.add(demoEntity2);
}
//2、list集合循环,实体为null就不操作实体
if (!CollectionUtils.isEmpty(list)) {//list不为空
list.forEach(l->{
if (null != l) {
System.out.println(l.getType());
}
});
}