我創建了一個List調用nodes_並初始化爲ArrayList, 當我添加一些東西給nodes_時,所有元素都爲null。 有一段代碼:arrayList中的所有元素在java中添加到arrayList時爲null
public class MyOocmdgGeneration1 {
private List<Node> nodes_ = new ArrayList<Node>();
public MyOocmdgGeneration1() {
findDependency();
}
private void findDependency() {
ClassNode c1 = new ClassNode();
FieldNode x = new FieldNode();
MethodNode c1Constructor = new MethodNode();
MethodNode m1 = new MethodNode();
nodes_.add(c1);
nodes_.add(x);
nodes_.add(c1Constructor);
nodes_.add(m1);
,並出現一種其他類:
public abstract class Node {
private String path;
private List<Node> callees;
private List<Node> callers;
private List<Node> children;
private Node parent;
private int id;
public Node() {
this.callees = new ArrayList<>();
this.callers = new ArrayList<>();
this.children = new ArrayList<>();
}
public Node(String path, List<Node> callees, List<Node> callers) {
this();
this.path = path;
this.callees = callees;
this.callers = callers;
this.children = new ArrayList<>();
}
public Node(int id, String path, Node parent) {
this();
this.id = id;
this.path = path;
this.parent = parent;
parent.addChild(this);
}
當我debuged,它看起來像這樣:debug image
Ohhhh,yeahhhhh,謝謝,我發現我的問題。它不是一個無效的數組列表 –