一个HashMap的遍历,我得到NullPointerException异常
可能重复:
When traverse a HashMap, I get NullPointerException一个HashMap的遍历,我得到NullPointerException异常
这是我的代码:
HashMap<String, HashSet<String>> map;
....//I didn't write those code.
System.out.println(map.entrySet().size()); // run util here is ok, I get the size of the map.
for(Map.Entry<String, HashSet<String>> entry : map.entrySet()) {// here throw the exception
}
我得到异常:
线程中的异常“main”java.lang.NullPointerException在key.test.EnwikiOutlink.main(EnwikiOutlink.java:68)
映射对象有超过10,000个映射对象,我在服务器机器上运行它并且无法调试。但是,当我缩小这张地图的尺寸(小于10,000)时,程序运行正常。问题的原因是什么,解决方案是什么?谢谢!
HashMap允许值为空。因此,在您的HashMap中,如果HashSet为空,您将得到NullPointerException。 确保HashSet不为空。
您的意思是条目(HashSet
'HashSet
所以'System.out'和for循环之间没有任何内容?你肯定会得到NPE的线路? – Thomas 2012-04-13 11:58:37