NPE(null point Exception)产生的场景

  1. public int f() {return Integer 对象},如果为 null,自动解箱抛 NPE。

  2. 数据库的查询结果可能为 null。

  3. 集合里的元素即使 isNotEmpty,取出的数据元素也可能为 null。

  4. 远程调用对象时,一律要求进行空指针判断,防止 NPE。

  5. 对于Session 中获取的数据,建议 NPE 检查,避免空指针。

  6. 级联调用 obj.getA().getB().getC();一连串调用,易产生 NPE。

猜你喜欢

转载自blog.csdn.net/u013310517/article/details/78219861