0

My node is having list of nodes(as children). ref.get(L.get(t)).children.add(N); L.get(t) is giving my parent node. ref.get(L.get(t)) is another node which I want to work with. I wanted to add my new node N to children of ref.get(L.get(t)). While debugging I can see a node in ref.get(L.get(t)) but throwing NullPointerException.

public void CreateNode(Node m,ArrayList<Node> S,HashMap<Node,Node> ref,ArrayList<String> L) {   
    Node N = new Node(m.val,m.data);
    ref.put(m,N);

    if(S.isEmpty()) {
        S.add(N);
    } else {
        Node c =ref.get(L.get(t));          //Showing C as null
        ref.get(L.get(t)).children.add(N);  //unable to access node in ref.get(L.get(t))
        System.out.println(ref.get(L.get(t)).val); 
        S.add(N);
        t++;
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Please use meaningful variable/parameter names. – Andreas Oct 24 '19 at 06:23
  • You are not accessing the `keySet` of the `Map`... You cannot get any key this way. Your code looks like C#-naming convention realized in Java... – deHaar Oct 24 '19 at 06:28

1 Answers1

0

Concerning your method signature, L is a list of String so L.get(int) will return a String. At the other hand, the keys for the ref Map are Node objects, so a lookup with a String will return a null. So, could either refactor your ref Map to have the unique String identifier of your Node or lookup by a Node object.

Andre Albert
  • 1,386
  • 8
  • 17