日期:2014-05-20  浏览次数:20762 次

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是new 出来的,虽然返回值为null,但本身是有指向的,调用方法怎么会抛异常啊,
  怎么判断才能不抛出异常

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

是一个意思,都不能代替 noAssessment != null

------解决方案--------------------
探讨

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