2012-04-13 63 views
0

可能重複:
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)時,程序運行正常。問題的原因是什麼,解決方案是什麼?謝謝!

+0

所以'System.out'和for循環之間沒有任何內容?你肯定會得到NPE的線路? – Thomas 2012-04-13 11:58:37

回答

0

HashMap允許值爲空。因此,在您的HashMap中,如果HashSet爲空,您將得到NullPointerException。 確保HashSet不爲空

+0

您的意思是條目(HashSet )爲空或HashSet 爲空? – janekey 2012-04-13 12:49:56

+1

'HashSet '可能爲空。 – 2012-04-13 12:55:41

相關問題