【MapSheep】
[好记性不如烂笔头]

报错:

  1. 日志
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

分析

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

解决方案

  1. 代码
// 原代码:
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));
  1. 就解决了
  • 需要注意其他的地方会不会影响到
posted on 2023-10-31 11:58  (Play)  阅读(706)  评论(0编辑  收藏  举报