public static List<Map<String,Object>> hour(List<Map<String,Object>> list){ if(list != null && list.size()>0){ String item=""; String [] s1 =null; for(Map<String,Object> m : list){ item = m.get("item_info").toString(); s1 = item.split(";"); for(String s : s1){ String [] s2 = null; s2=s.split(","); m.put(s2[0],s2[2]); m.put(s2[0]+"code",s2[0]); m.put(s2[0]+"detname",s2[3]); if(s2.length>=5) { m.put(s2[0] + "bzz", s2[4]); } if(s2.length>=6) { m.put(s2[0] + "yj", s2[5]); } if(s2.length>=7) { m.put(s2[0] + "bj", s2[6]); } if(s2.length>=8) { m.put(s2[0] + "dw", s2[7]); } } } } return list; } 会报空指针异常 如何改正
时间: 2024-03-08 22:50:22 浏览: 86
List<map>多字段组合排序
5星 · 资源好评率100%
可以在代码中加入对 `s2` 是否为 `null` 的判断,如下所示:
```
public static List<Map<String, Object>> hour(List<Map<String, Object>> list) {
if (list != null && list.size() > 0) {
String item = "";
String[] s1 = null;
for (Map<String, Object> m : list) {
item = m.get("item_info").toString();
s1 = item.split(";");
for (String s : s1) {
String[] s2 = null;
s2 = s.split(",");
if (s2 != null && s2.length > 2) {
m.put(s2[0], s2[2]);
m.put(s2[0] + "code", s2[0]);
m.put(s2[0] + "detname", s2[3]);
if (s2.length >= 5) {
m.put(s2[0] + "bzz", s2[4]);
}
if (s2.length >= 6) {
m.put(s2[0] + "yj", s2[5]);
}
if (s2.length >= 7) {
m.put(s2[0] + "bj", s2[6]);
}
if (s2.length >= 8) {
m.put(s2[0] + "dw", s2[7]);
}
}
}
}
}
return list;
}
```
在对 `s2` 进行操作前,加入了 `s2 != null && s2.length > 2` 的判断,确保了 `s2` 不为 `null` 且至少有 3 个元素,避免了空指针异常的发生。
阅读全文