【问题标题】:Why I get a nullPointerExeption? [duplicate]为什么我得到一个 nullPointerException? [复制]
【发布时间】:2026-01-16 13:50:02
【问题描述】:

我希望你能帮助我。当我想将 NodeList / Node 放入我的地图时,我得到一个 NullPointerExeption:

Map <String, NodeList> config = null; 

public void loadConfiguration() {
    helper helper = new helper();
    NodeList nodes = null;
    nodes = helper.getXPathFromFile("/root/*", "conf/config.xml");

    if (nodes.getLength() > 0) {
        for (int i = 0; i < nodes.getLength(); i++) {
            String NodeName = nodes.item(i).getNodeName();
            NodeList NodeItem = (NodeList) nodes.item(i);
            System.out.println(NodeName); // Here the right Name puts out
            System.out.println(helper.nodelistToString(NodeItem)); // here right inside XML-Code put out
            config.put(NodeName, NodeItem); // Here comes the NullPointerEx.
        }
    }
}

【问题讨论】:

  • 调试总是有帮助的! :)

标签: java xmlnode xmlnodelist


【解决方案1】:

您的地图confignull,这就是您获得NullPointerException 的原因。你应该初始化它,例如Map &lt;String, NodeList&gt; config = new HashMap &lt;String, NodeList&gt;();

【讨论】:

  • 感谢您的帮助!!!
【解决方案2】:

您似乎从未初始化您的 config 映射。

改变

Map <String, NodeList> config = null; 

Map <String, NodeList> config = new HashMap<String, NodeList>; 

【讨论】:

  • 这样的问题值得回答吗?
最近更新 更多