【錯誤】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