for(int i=0 ;i<employees.size();i++){
    Employee e = employees.get(i).getMajor();
//     System.out.println(e.getEmpName());//这句话在上面执行就有空指针错误,在下面执行就没错误
    if (e instanceof Employee){
                  System.out.println(e.getEmpName());
       if (e.getEmpName().equals(condition)){
 results.add(employees.get(i));
       }
    }
//
}

解决方案 »

  1.   

    Employee e = employees.get(i).getMajor();右侧的返回的是什么?
      

  2.   

    就是啊,我也很疑惑,可能是instanceof有什么蹊跷吧
      

  3.   

    to:liuyuhua0066
       右侧是返回一个Employee类型的值
        Employee major;
      

  4.   

    Employee e = employees.get(i).getMajor() 这句不报错?
      

  5.   

    是啊,不报错,
    System.out.println(e.getEmpName());//这句编译没错,但执行时报空指针错误
      

  6.   

    System.out.println(e.getEmpName());//这句编译没错,但执行时报空指针错误(放到下面if e instanceof Employee)里面执行时就没错
      

  7.   

    if (e instanceof Employee){//如果e==null, e instanceof Employee会return false看这里:http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.21. The type of a RelationalExpression operand of the instanceof operator must be a reference type or the null type2. At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
      

  8.   

    如果是null if都没进入怎么可能有错误