【错误】element cannot be mapped to a null key


element cannot be mapped to a null key的解决方法

报错:

ERROR [o.a.c.c.C.[.[.[/sa].[dispatcherServlet]] - Servlet.service() for servlet [dispatcherServlet] in context with path [/sa] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: element cannot be mapped to a null key] with root cause

java.lang.NullPointerException: element cannot be mapped to a null key

简单来说,要加一个非空的过滤

原代码:

  Map<String, List<Device>> deviceMap = deviceList.stream().collect(Collectors.groupingBy(Device::getIp));

增加一个非空判断

  Map<String, List<Device>> deviceMap = deviceList.stream()
  .filter(item-> StringUtils.isNotBlank(item.getIp())).collect(Collectors.groupingBy(Device::getIp));

就解决了

需要注意其他的地方会不会影响到


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM