-4

我希望你能帮助我。当我想将 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.
        }
    }
}
4

2 回答 2

1

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

于 2014-11-10T12:44:36.947 回答
0

看起来你从来没有初始化你的config地图。

改变

Map <String, NodeList> config = null; 

Map <String, NodeList> config = new HashMap<String, NodeList>; 
于 2014-11-10T12:44:29.370 回答