为什么我得到nullPointerException?

时间:2014-11-10 12:43:45

标签: java xmlnode xmlnodelist

我希望你能帮助我。当我想将NodeList / Node放到我的Map:

时,我得到一个NullPointerException
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.
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您似乎永远不会初始化config地图。

更改

Map <String, NodeList> config = null; 

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

答案 1 :(得分:0)

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