NullPointerException-----开发中遇到的空指针异常
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-----开发中遇到的空指针异常的更多相关文章
- Android开发中,那些让您觉得相见恨晚的方法、类或接口
Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...
- Android应用开发中关于this.context=context的理解
在Android应用开发中,有的类里面需要声明一个Context的成员变量,然后还需要在该类的构造函数中加上this.context=context;这行代码.为什么要这么写呢?不写不行么? 先看下面 ...
- JAVA开发中遇到的异常总结
最常见的五种异常:必会,面试题: 算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数 ...
- Android开发中的输入合法性检验
Why ? 合法性检查对于程序的健壮性具有重要作用.在Android开发中,良好的合法性检查设计机制可以使程序更加清晰,产生bug更少,交互更加友好. What ? 合法性检查的目的在于确定边界.对于 ...
- android开发中遇到的各种问题收集--不定期更新
以下问题都是自己在开发中亲身碰到的 ,在这里留个备份,方便下次查阅. 1.java.lang.IllegalStateException ,Cannot execute task: the task ...
- Android开发中常见的设计模式
对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...
- Java 开发中如何正确踩坑
为什么说一个好的员工能顶 100 个普通员工 我们的做法是,要用最好的人.我一直都认为研发本身是很有创造性的,如果人不放松,或不够聪明,都很难做得好.你要找到最好的人,一个好的工程师不是顶10个,是顶 ...
- 记一次SpringBoot 开发中所遇到的坑和解决方法
记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...
- IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化:
IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化: IT蓝豹 ------------------> sqlite数据库版本升级 1.sqlite升级步骤: 1.自己写一个类继承自 ...
- EBS OAF开发中的错误/异常处理(ErrorHandling) (转)
原文地址 EBS OAF开发中的错误/异常处理(ErrorHandling) EBS OAF开发中的错误/异常处理(ErrorHandling) (版权声明,本人原创或者翻译的文章如需转载,如转载用于 ...
随机推荐
- C#反射の反射详解
C#反射の反射详解(点击跳转)C#反射の反射接口(点击跳转)C#反射反射泛型接口(点击跳转)C#反射の一个泛型反射实现的网络请求框架(点击跳转) 一.什么是反射 反射(Reflection):这是.N ...
- POJ - 2151 (概率dp)
题意:有T个队伍,有M道题,要求每个队至少有一道题,并且有队伍至少过N道题的概率. 这个题解主要讲一下,后面的,至少有一道题解决和至少一道题至N-1道题解决,到底怎么算的,其实,很简单,就是母函数. ...
- laravel 使用构造器进行增删改查
使用原生语句进行增删改查 //$list = DB::select('select * from wt_category where id = :id', ['id' => 34]); //$i ...
- go标准库的学习-net/http
参考:https://studygolang.com/pkgdoc 概念解释: request:用户请求的信息,用来解析用户的请求信息,包括post.get.cookie.url等信息 respons ...
- Java/JSP程序连接不上Mysql驱动问题解决方法
错误提示: java.lang.ClassNotFoundException: com.mysql.jdbc.Driverat java.net.URLClassLoader$1.run(URLCla ...
- stm32 中断号和中断处理函数建立关系
转载:https://www.cnblogs.com/heny-hui/p/7130620.html stm32的中断号根据不同内核和型号,st公司给的官方库中对相应的中断号进行了设置,我们用到哪一个 ...
- Android学习之基础知识四-Activity活动7讲(活动的启动模式)
在实际的项目开发中,我们需要根据特定的需求为每个活动指定恰当的启动模式.Activity的启动模式一共有4种:standard.singleTop.singleTask.singleInstance. ...
- JavaScript中的slice函数
String.slice(start,end)returns a string containing a slice, or substring, of string. It does not mod ...
- python 经典博客链接
1, 从文件的读取与输出: http://www.cnblogs.com/xuxn/archive/2011/07/27/read-a-file-with-python.html http://www ...
- HDMI传输原理:TMDS
参考资料:http://blog.sina.com.cn/s/blog_679686370100vgg1.html: http://www.eeworld.com.cn/mndz/2011/0818/ ...