对于一项作业,我遇到了一个问题。我会告诉您我正在做什么。

作业需要我创建一个Java小程序(即使我使用的是JFrame,哎呀),也要以文本形式从用户那里获取输入。使用此文本,程序将在每个单词长度中显示多少个单词,并在单独的窗口中以图形的形式显示结果。我的哈希图包含两个Integer,单词的长度以及该长度有多少。

但是,该分配要求用户能够上载两个文本文件并比较结果,因此我必须发送一个哈希图数组,每个哈希图具有单独的结果到窗口。我一直这样做:

public void dispatchNewGraph (ArrayList<Map<Integer, Integer>> sortedCount) {
    TextAnalyserGraph graphFrame = new TextAnalyserGraph(sortedCount);
}


在打开的窗口中,传递给构造函数的数据将应用于TextAnalyserGraph类private ArrayList<Map<Integer, Integer>> graphData;中的此属性。

我的问题是,当我尝试使用以下代码迭代此HashMaps数组时,出现了nullpointerexception错误:

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)

最佳答案

循环之前,您需要对graphData进行空检查。

我还建议修改您的for循环,使其更好一些。

如果您关心这些键,可以使用它:

        if(graphData != null){
            for(Map<Integer, Integer> valueMap : graphData){
                for (Map.Entry<Integer, Integer> entry : valueMap.entrySet()) {
                    if (entry.getValue() > largest)
                        largest = entry.getValue();
                    bars++;
                }
            }
        }


但是由于您不关心密钥,并且密钥已在循环逻辑中使用,因此我建议:

    if(graphData != null){
        for(Map<Integer, Integer> valueMap : graphData){
            for (Integer value : valueMap.values()) {
                if (value > largest)
                    largest = value;
                bars++;
            }
        }
    }

07-24 09:51