Arraylist 调用 isempty 与size抛出空指针异常

List<Store> noAssessment = new ArrayList<Store>();
noAssessment = storeBo.getNoNeededAssessment(user.getId());//返回值为空

System.out.println("noAssessment" + noAssessment);
List<Order> noAssessmentOrder = new ArrayList<Order>();

if(noAssessment.size() > 0)或if(!noAssessment.isempty)都会抛出空指针异常

 

解决:noAssessment = storeBo.getNoNeededAssessment(user.getId());
重新指向了null,不在指向原来的new ArrayList<Store>()了
null.size();所以抛异常

正解 noAssessment 赋值为空,所以释放了原来的对象,所以还是先加个判断是否为空。
noAssessment != null&&noAssessment.size() != 0

你可能感兴趣的:(java)