1.使用CollectionUtils.isEmpty判断空集合

public class TestIsEmpty {
static class Person{}
static class Girl extends Person{} public static void main(String[] args) {
List<Integer> first = new ArrayList<>();
List<Integer> second = null;
List<Person> boy = new ArrayList<>();
boy.add(new Girl());
System.out.println(CollectionUtils.isEmpty(first));//true
System.out.println(CollectionUtils.isEmpty(first));//true
System.out.println(CollectionUtils.isEmpty(first));//false System.out.println(first==null);//false
System.out.println(second==null);//true
System.out.println(boy==null);//false System.out.println(first.size()); //size=0
System.out.println(second.size());// NullPointerException
System.out.println(boy.size()); //size=1 System.out.println(first.isEmpty()); //true
System.out.println(second.isEmpty()); //NullPointerException
System.out.println(boy.isEmpty()); //false 所以:
isEmpty() 或者(list.size() == 0)用于判断List内容是否为空,即表里一个元素也没有
但是使用isEmpty()和size()的前提是,list是一个空集合,而不是null,
所以为了避免异常,建议在使用或赋值list集合之前,做一次空集合创建处理,进行内存空间分配,即: List list2 = new ArrayList()
判断不为空的情况: list!=null && !list.isEmpty()
}
}

二 .容易出现空指针的地方

1.实体类里定义的集合

public class Org {
private Long id;
private String title;
private Long parent;
private List<User> list; public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
public Long getParent() {return parent;}
public void setParent(Long parent) {this.parent = parent;}
public List<User> getList() {
if(list==null){
list=new ArrayList<>(); //这里最好对这个集合处理一下,防止报空指针
}
return list;
} public void setList(List<User> list) {
this.list = list;
}
}

2.自己定义的集合接收数据库查询的数据

如:List<Org> users=userService.getAll();
如果查询结果为空,users.size=0.users是一个空集合,但不是空
使用前最好判断一下是不是为空

NullPointerException-----开发中遇到的空指针异常的更多相关文章

  1. Android开发中,那些让您觉得相见恨晚的方法、类或接口

    Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...

  2. Android应用开发中关于this.context=context的理解

    在Android应用开发中,有的类里面需要声明一个Context的成员变量,然后还需要在该类的构造函数中加上this.context=context;这行代码.为什么要这么写呢?不写不行么? 先看下面 ...

  3. JAVA开发中遇到的异常总结

    最常见的五种异常:必会,面试题: 算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数 ...

  4. Android开发中的输入合法性检验

    Why ? 合法性检查对于程序的健壮性具有重要作用.在Android开发中,良好的合法性检查设计机制可以使程序更加清晰,产生bug更少,交互更加友好. What ? 合法性检查的目的在于确定边界.对于 ...

  5. android开发中遇到的各种问题收集--不定期更新

    以下问题都是自己在开发中亲身碰到的 ,在这里留个备份,方便下次查阅. 1.java.lang.IllegalStateException ,Cannot execute task: the task ...

  6. Android开发中常见的设计模式

    对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...

  7. Java 开发中如何正确踩坑

    为什么说一个好的员工能顶 100 个普通员工 我们的做法是,要用最好的人.我一直都认为研发本身是很有创造性的,如果人不放松,或不够聪明,都很难做得好.你要找到最好的人,一个好的工程师不是顶10个,是顶 ...

  8. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  9. IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化:

    IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化: IT蓝豹 ------------------> sqlite数据库版本升级 1.sqlite升级步骤: 1.自己写一个类继承自 ...

  10. EBS OAF开发中的错误/异常处理(ErrorHandling) (转)

    原文地址 EBS OAF开发中的错误/异常处理(ErrorHandling) EBS OAF开发中的错误/异常处理(ErrorHandling) (版权声明,本人原创或者翻译的文章如需转载,如转载用于 ...

随机推荐

  1. sql server2008 R2打开报错:无法识别的配置节 system.serviceModel解决办法分享

    本人是先安装的sql server2008 R2成功可以运行后,再安装VS2010成功后,再打开sql server2008,就出现以下错误,无法连接服务器.   无法识别的配置节 system.se ...

  2. python五十七课——正则表达式(边界字符)

    演示匹配锚字符(边界字符)^:从字符串头部开始匹配,在开启多行模式下(re.M),可以尝试匹配每一行的头部数据$:从字符串尾部开始匹配,在开启多行模式下(re.M),可以尝试匹配每一行的尾部数据A:从 ...

  3. Spark本地运行成功,集群运行空指针异。

    一个很久之前写的Spark作业,当时运行在local模式下.最近又开始处理这方面数据了,就打包提交集群,结果频频空指针.最开始以为是程序中有null调用了,经过排除发现是继承App导致集群运行时候无法 ...

  4. Hue添加Spark notebook

    参考自https://blogs.msdn.microsoft.com/pliu/2016/06/18/run-hue-spark-notebook-on-cloudera/ 说明 使用Clouder ...

  5. Codeforces Round #553 (Div. 2) D. Stas and the Queue at the Buffet 贪心+公式转化

    题意 给出n个pair (a,b) 把它放在线性序列上 1--n 上 使得  sum(a*(j-1)+b*(n-j))  最小 思路 :对式子进行合并 同类项 有:    j*(a-b)+  (-a+ ...

  6. ethereum/EIPs-1077 Executable Signed Messages

    https://github.com/alexvandesande/EIPs/blob/ee2347027e94b93708939f2e448447d030ca2d76/EIPS/eip-1077.m ...

  7. Ubuntu sudo apt-get 安装下载更新软件包命令详解

    sudo apt-get install package                    安装软件包sudo apt-get install package - - reinstall 重新安装 ...

  8. C语言的基本数据类型

     代码如下: // text.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" int main() { int a; //告诉编译器,分配 4 ...

  9. Kubernetes 1.10.4 镜像 版本

    1. gcr.io/google-containers/hyperkube:1.10.4 gcr.io/google_containers/pause-amd64:3.0 gcr.io/google_ ...

  10. 使用Windows API进行串口编程

    使用Windows API进行串口编程   串口通信一般分为四大步:打开串口->配置串口->读写串口->关闭串口,还可以在串口上监听读写等事件.1.打开和关闭串口Windows中串口 ...