除了try{}catch{},你究竟还知道多少避免空指针异常的骚操作?

25 篇文章 1 订阅
13 篇文章 1 订阅

        你究竟还知道多少避免空指针异常的那些骚操作?

        一个有趣的bug总能找到一个有趣的java8特性!当我们遇到空指针异常时,你还在使用try...catch吗?那就太out啦!这里教你一招使用java8特性来避免空指针异常。

        小伙伴知直呼——装到啦!


目录

1. 错误分析

2. 第一步解决方案

2.1 解决过程

2.1 过程分析

3. 第二步解决方案

2.1 解决过程

2.1 过程分析

4. 使用java8特性后

5. 总结


        我们从一个staff案例开始,这里的类结构我们为了演示暂时随便写(不要介意),在构建的Test类中实例化这个Staff,并依次调用方法,获取员工的地址编码! 

public class Test {

	public static void main(String[] args) {
		Staff staff=new Staff();
		String code=staff.getAdd().getCity().getCode().getCodeMsg();
		System.out.println(code);
	}

}

        但是run后发现他报错了,太正常了!

class Staff {
	int uid;
	String uname;
	Address add;
	public Staff(int uid, String uname, Address add) {
		super();
		this.uid = uid;
		this.uname = uname;
		this.add = add;
	}
	public Staff() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * @return the uid
	 */
	public int getUid() {
		return uid;
	}
	/**
	 * @param uid the uid to set
	 */
	public void setUid(int uid) {
		this.uid = uid;
	}
	/**
	 * @return the uname
	 */
	public String getUname() {
		return uname;
	}
	/**
	 * @param uname the uname to set
	 */
	public void setUname(String uname) {
		this.uname = uname;
	}
	/**
	 * @return the add
	 */
	public Address getAdd() {
		return add;
	}
	/**
	 * @param add the add to set
	 */
	public void setAdd(Address add) {
		this.add = add;
	}
	
}
class Address {
	int id;
	City city;
	
	public Address() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Address(int id, City city) {
		super();
		this.id = id;
		this.city = city;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the city
	 */
	public City getCity() {
		return city;
	}

	/**
	 * @param city the city to set
	 */
	public void setCity(City city) {
		this.city = city;
	}
	
}
class City {
	Code code;
	String name;
	public City() {
		super();
	}
	public City(Code code, String name) {
		super();
		this.code = code;
		this.name = name;
	}
	public Code getCode() {
		return code;
	}
	public void setCode(Code code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}
class Code {
	int id;
	String codeMsg;
	public Code() {
	}
	public Code(int id, String code) {
		super();
		this.id = id;
		this.codeMsg = code;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the code
	 */
	public String getCodeMsg() {
		return codeMsg;
	}
	/**
	 * @param code the code to set
	 */
	public void setCodeMsg(String code) {
		this.codeMsg = code;
	}
	
}

1. 错误分析

        main方法在staff调用过程链中的每个环节中都存在空对象!

2. 第一步解决方案

2.1 解决过程

        main方法中,在调用方法前对调用链中的环节进行判断。

public static void main(String[] args) {


		Staff staff=new Staff();

        if(staff.getAdd().getCity().getCode().getCodeMsg() == null)
            return;
		String code=staff.getAdd().getCity().getCode().getCodeMsg();
		System.out.println(code);
	}

运行结果仍然出错了:

2.1 过程分析

显而易见,这样是错误的,调用链中,如:

  1. staff可能为空
  2. staff.getAdd()可能为空
  3. staff.getAdd().getCity().getCode()也可能为空

所以这样的判断条件是不充分的,仍然存在空指针异常。

3. 第二步解决方案

2.1 解决过程

根据第一步解决办法我们进行完善判断条件得到第二步解决办法:

public static void main(String[] args) {
		Staff staff=new Staff();
		
		if(staff==null ||
			staff.getAdd() == null ||
			staff.getAdd().getCity() == null ||
			staff.getAdd().getCity().getCode() == null ||
			staff.getAdd().getCity().getCode().getCodeMsg() == null){
            System.out.println("不会出现空指针异常");
			return;
		}
		String code=staff.getAdd().getCity().getCode().getCodeMsg();
		System.out.println(code);
	}

运行结果:

2.1 过程分析

        明显,运行后并没有出现新的空指针异常了,但是这个判断过程实在是比较繁琐,好多同学反映这样不太友好,他说:“如果这个调用链好长怎么办?写这么多判断条件不友好啊!”

4. 使用java8特性后

        根据同学们的反馈,我们直接来使用java8为我们提供的避免空指针异常的新特性:

public static void main(String[] args) {
		Staff staff=new Staff();
		
		String code=Optional.ofNullable(staff)
				.map(Staff::getAdd)
				.map(Address::getCity)
				.map(City::getCode)
				.map(Code::getCodeMsg)
				.orElse(null);
//				staff.getAdd().getCity().getCode().getCodeMsg();
		System.out.println(code);
	}

运行结果,发现是不会出现空指针异常的:

5. 总结

        其实,在日常工作学习中不断地探索和发现一些Java8特性是一个非常有意义的过程!

        但是我们不可不知的是,无论在什么情况下,一个空指针异常或许是一件不能去避免的过程,如果强行掩盖一个这样的问题,那么迎接的问题可能会更加严重,一句老话说得好,叫“丢了西瓜,捡了芝麻”,敲代码就是不断发现错误和解决错误的一个过程,有时候使用某些方法就行这里一样去避免这种空指针异常一样,我们需要一个工具来避免异常的发生。

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值