我的节点有节点列表(作为子节点)。
ref.get(L.get(t)).children.add(N);
L.get(t)
正在给我的父节点。 ref.get(L.get(t))
是我想要使用的另一个节点。
我想添加我的新节点 N
致 ref.get(L.get(t))
的 child 。调试时我可以在 ref.get(L.get(t))
中看到一个节点但扔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++;
}
}
最佳答案
关于您的方法签名,L 是一个 String
列表,因此 L.get(int) 将返回一个 String
。另一方面,ref Map
的键是 Node
对象,因此使用 String
进行查找将返回 null。
因此,可以重构您的 ref Map 以具有 Node 的唯一字符串标识符,也可以通过 Node 对象进行查找。
关于java - 访问作为值存储在 HashMap 中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535192/