小编ASH*_*ISH的帖子

如果list在这种情况下为空,我怎样才能避免java中的空指针异常?

public static void selectEMployee() {
   if (list1.isEmpty()) {
         System.out.println("The list is empty");
   }else {
         System.out.println("The list of employees are");
         for (Employee emp : list1) {
           System.out.println("Name::" + emp.getName() + "\t EmpId::"
                            + emp.getEmpid() + "\t Address::" + emp.getAddress()
                            + "\tphone::" + emp.getPhone());
         }       
    }
}
Run Code Online (Sandbox Code Playgroud)

当列表为空时我想显示"列表为空"但它会抛出异常?

java collections nullpointerexception

1
推荐指数
1
解决办法
5896
查看次数

标签 统计

collections ×1

java ×1

nullpointerexception ×1