-4

I hope you can help me. I get an NullPointerExeption when I want to Put the NodeList / Node to my Map:

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.
        }
    }
}
Burner
  • 765
  • 13
  • 37

2 Answers2

1

Your map config is null, that's why you get the NullPointerException. You should initialize it e.g. Map <String, NodeList> config = new HashMap <String, NodeList>();

peter.petrov
  • 36,377
  • 12
  • 75
  • 137
0

It looks like you never initialize your config map.

Change

Map <String, NodeList> config = null; 

to

Map <String, NodeList> config = new HashMap<String, NodeList>; 
Eran
  • 374,785
  • 51
  • 663
  • 734