zoukankan      html  css  js  c++  java
  • List列表排序报空指针异常

      在项目中,想对列表数据按照日期字段排序,使用List.sort(Comparator c)方法,却总是报空指针异常。

      通过网上查询,得知该问题应该是List中有null值引起的。即ArrayList中的数组长度默认为10,如果对其添加值个数小于10,就会有null值存在。而sort()方法会遍历每个对象取该对象的日期字段,当对象为null时再取其日期字段值自然会报空指针异常。

      下面是网友的解决方法,新建一个同类型的ArrayList来装null,然后再从原List中removeAll该新List。这样一来原List中的null值就都不见了。排序也就不再报空指针异常了。原文:https://blog.csdn.net/liming_0820/article/details/77744240

    List<CurriclumScheduleEntityModel> Nonlist = new ArrayList<CurriclumScheduleEntityModel>();  
    Nonlist.add(null);  
    listCellTime.remove(i);  
    listCellTime.removeAll(Nonlist);
    
    ---------------------
    作者:LiMing_0820 
    来源:CSDN 
    原文:https://blog.csdn.net/liming_0820/article/details/77744240?utm_source=copy 
    版权声明:本文为博主原创文章,转载请附上博文链接!
  • 相关阅读:
    2251: [2010Beijing Wc]外星联络
    1500 后缀排序
    1492: [NOI2007]货币兑换Cash【CDQ分治】
    P3380 【模板】二逼平衡树(树套树)
    python opencv
    pycharm调试
    pycharm中选择python interpreter
    创建使用pycharm virtualenv
    reload函数
    python3编写发送四种http请求的脚本
  • 原文地址:https://www.cnblogs.com/GreenMountain/p/9766841.html
Copyright © 2011-2022 走看看