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));

就解决了

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

相关文章:

  • 2022-12-23
  • 2021-08-27
  • 2021-09-26
  • 2021-04-21
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2022-01-21
  • 2021-07-07
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
相关资源
相似解决方案