当前位置:   article > 正文

set集合add(),空指针异常处理_set addall空指针

set addall空指针

操作逻辑的时候,使用了set集合,但是出现了空指针,排除了很久,发现有一个低级错误,没有初始化set,导致没有set对象,所以无法使用set的方法

  Set<String> starHeadUrl = null;

        //如果超过三个,就显示前三个,如果不够三个,就全部显示
        if(listAllFriendsVos1.isPresent()){
            List<AppendMomentStarVo> listAllNOFriendsVos = listAllFriendsVos1.get();
            int i = listAllNOFriendsVos.size();

            Map<String, AppendMomentStarVo> collect =
                    listAllNOFriendsVos.stream().collect(Collectors.toMap(AppendMomentStarVo::getHeadurl,
                            Function.identity(),(key1, key2) -> key2));

            int count = configLogic.getValue2Integer(ConfigLogic.COUNT_STAR_HEADURL);
            logger.info("listNOMomentstar:count{}::i{}",count,i);
            if(i<=count){
                starHeadUrl = collect.keySet();
            }else{
                Set<String> set = collect.keySet();
                String[] arr = new String[set.size()];
                String[] strings = set.toArray(arr);
                logger.info("string.length:{}",strings.length);
                starHeadUrl = new TreeSet<>();
                for (int a=0;a<count;a++){
                    starHeadUrl.add(strings[a]);
                }
            }
  • 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

错误的地方:
这里写图片描述
如果在【2】位置没有初始化set,,那么,在添加的时候会出现空指针,因为这个时候的set是null,没有初始化,所以对象不存在,是空的!!!!!
在初始化了set之后,就可以正常添加了。

引以为戒!!!!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/141716
推荐阅读
相关标签
  

闽ICP备14008679号