• 空指针异常处理


    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();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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))
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    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
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    4.赋初值 不要返回null

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

    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; // 空指针异常
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    当变量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: 不能为空
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.optional

  • 相关阅读:
    【mycat】mycat安装
    MacOS安装conda
    Java 中 Comparator 接口的使用
    基于标签的电影推荐算法研究_张萌
    【Go实现】实践GoF的23种设计模式:迭代器模式
    无法打开包括文件: “libxml/xpath.h”: No such file or directory
    Hadoop3:HDFS-查看logs文件,排查NameNode故障原因。
    消息中间的应用场景
    华为认证云计算专家(HCIE-Cloud Computing)--问答题
    OSEK标准ISO_17356汇总介绍
  • 原文地址:https://blog.csdn.net/weixin_46666822/article/details/127747154