對於我遇到問題的任務。爲了提升一點,我會告訴你我正在做什麼。Java迭代/ foreach包含散列表/映射的ArrayList <Int, Int>
該任務需要我創建一個java applet(即使我使用的是JFrame
,oops),它以一段文本的形式從用戶獲取輸入。有了這段文字,程序會將每個單詞長度有多少,並以單獨的窗口以圖形的形式顯示結果。我的散列表包含兩個Integer
s,單詞的長度以及該長度有多少。
然而,這個任務要求用戶能夠上傳兩個文本文件並比較結果,因此我必須發送一個hashmaps數組,每個hashash都有單獨的結果到窗口。我一直在做的是這樣的:
public void dispatchNewGraph (ArrayList<Map<Integer, Integer>> sortedCount) {
TextAnalyserGraph graphFrame = new TextAnalyserGraph(sortedCount);
}
並在打開的窗口中,傳遞給構造函數的數據應用到這個屬性在TextAnalyserGraph類private ArrayList<Map<Integer, Integer>> graphData;
我的問題是,我得到一個nullpointerexception
錯誤,當我嘗試遍歷包含HashMap的這陣用下面的代碼:
for(Iterator<Map<Integer, Integer>> i = graphData.iterator(); i.hasNext();) {
Map<Integer, Integer> graph = i.next();
for (Integer value : graph.keySet()) {
if(graph.containsKey(value)) {
Integer v = graph.get(value);
if (v.intValue() > largest)
largest = v.intValue();
}
bars++;
}
}
錯誤在這最後的代碼塊的第一行開始。如果我將graphData移動到下一行,那麼同樣的錯誤開始指出graphData的問題,我真的很難在這方面取得進展!
非常感謝! :)
堆棧跟蹤
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TextAnalyserGraph.paint(TextAnalyserGraph.java:47)
at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
帖子堆棧跟蹤。 –
'但是這個任務要求用戶能夠上傳兩個文本文件並比較結果,因此我必須發送一個hashmaps數組,每個hashash都有單獨的結果。如果你知道只有2個Maps,爲什麼你需要一個數組嗎?爲什麼不使用Pair,或者將2個地圖作爲單獨的參數傳遞。 – Falmarri
當我們不知道行號時,stacktrace並沒有太多幫助:) – ninnemannk