空指针异常处理

1.对象判断空

java.util.Objects

    @Test
    public void test01(){
        Object object = null;
        System.out.println(Objects.isNull(object));//true
        System.out.println(Objects.nonNull(object)); //false
        //Objects.requireNonNull(object,"该对象不能为空");//java.lang.NullPointerException: 该对象不能为空
        object = 1;
        Object requireNonNull = Objects.requireNonNull(object, "不能为空");//不为空的话会返回当前对象,这里返回Integer 值为1
        System.out.println();
    }

2.字符串判断空

org.apache.commons.lang3.StringUtils

    @Test
    public void test02(){
        String s1 = null;
        String s2 = "";
        String s3 = " ";
        //  StringUtils.isEmpty  字符串对象是否为空且长度为0 仅有空格的话有长度 会判断为不为空
        System.out.println(StringUtils.isEmpty(s1)); //true
        System.out.println(StringUtils.isEmpty(s2)); //true
        System.out.println(StringUtils.isEmpty(s3)); //false
        System.out.println(s3.length());
        // isNotEmpty 和isEmpty 校验结果相反

        // StringUtils.isBlank、StringUtils.isNotBlank 会将字符串开头,结尾的空格去掉之后,判断长度是否大于0
        System.out.println(StringUtils.isBlank(s3)); //这条会变为true

        //字符串比较 要将常量放到前面
        //if ("ehang".equals(name))
    }

3.集合判断为空

Map、List、Set 是经常会用到的数据结构,虽然他们都包含有isEmpty()方法,能判断容器中是否包含了元素,但是无法判断自生对象是否为空,一旦对象没有实例化时,调用isEmpty()就会报空指针异常;Spring 为我们提供了一个org.springframework.util.CollectionUtils工具类,其中的isEmpty就会优先判断对象是否为空,然后再通过isEmpty()判断是否存在元素,能大大减少因为对象为空带来的空指针异常;

    @Test
    public void test03(){
        Map map = null;
        //System.out.println(map.isEmpty()); // 空指针异常
        System.out.println(CollectionUtils.isEmpty(map)); // true
        map = new HashMap();
        System.out.println(map.isEmpty()); // true
        System.out.println(CollectionUtils.isEmpty(map)); // true
        map.put("1", "2");
        System.out.println(CollectionUtils.isEmpty(map)); // false
        System.out.println(map.isEmpty()); // false

        List list = null;
        //System.out.println(list.isEmpty()); // 空指针异常
        System.out.println(CollectionUtils.isEmpty(list)); // true
        list = new ArrayList();
        System.out.println(list.isEmpty()); // true
        System.out.println(CollectionUtils.isEmpty(list)); // true
        list.add("1");
        System.out.println(CollectionUtils.isEmpty(list)); // false
        System.out.println(list.isEmpty()); // false

        Set set = null;
        //System.out.println(set.isEmpty()); // 空指针异常
        System.out.println(CollectionUtils.isEmpty(set)); // true
        set = new TreeSet();
        System.out.println(set.isEmpty()); // true
        System.out.println(CollectionUtils.isEmpty(set)); // true
        set.add("1");
        System.out.println(CollectionUtils.isEmpty(set)); // false
        System.out.println(set.isEmpty()); // false
    }

4.赋初值 不要返回null

		//当定于局部变量,定义对象的属性时,能赋初始值的就尽量带上初始值;
        Map map = new HashMap();
        Integer integer =0;
		//当方法有返回值的时候,非必要的情况下,尽量不要返回null;
        Collections.emptyList(); //比如返回list 如果要返回空list 可以这样返回 而不是返回null

5.局部变量使用基本数据类型

    public void test04(){
        int x;
        Integer y;
        //System.out.println( x + 1 );  // 编译失败
        //System.out.println( y + 1 );  // 编译失败

        int i = 1;
        Integer j = null;
        System.out.println( i + 1 );  // 正常
        System.out.println( j + 1 );  // 空指针异常
        int m = i; // 正常
        int n = j; // 空指针异常
    }

当变量x、y 只定义、不赋值的时候,x + 1 和 y + 1 是没办法通过编译的;**而包装类 j 指定为null对象,当包装类参与运算的时候,首先会做拆箱操作,也就是调用 intValue() 方法,由于对象是空的,调用方法自然就会报空指针;同时,将一个包装类赋值给一个基本数据类型时,同样也会做拆箱操作,自然也就空指针异常了;**但是,基本数据类型就必须指定一个具体值,后续不管运算、还是赋值操作,都不会出现空指针异常;

6.断言

Assert 用来断定某一个实际的运行值和预期项是否一致,所以他和其他工具类的校验方式是反着在;比如isNull方法是期望对象为null,如果不为空的时候,就会报错;notNull表示期望对象不为空,当对象为空时,就会报错;

正常使用时需要处理这个异常

    @Test
    public void test06(){
        Integer i1 =null;
        Assert.notNull(i1,"不能为空"); //java.lang.IllegalArgumentException: 不能为空
    }

7.optional

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值