簡體   English   中英

當添加到Java中的arrayList時,arrayList中的所有元素為null

[英]All elements in arrayList is null when add to arrayList in java

當我向nodes_添加內容時,我創建了一個List調用nodes_並初始化為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);
}

當我調試時,它看起來像這樣: debug image

您看到的( "null" )是在每個這些類上調用的toString()的輸出。 它們實際上不是空的。

你怎么知道? 調試器顯示每個條目,例如ClassNode@464 那是指一個特定的實例,而不是null。

節點不為空。 對象全部存在,但是它們的toString表示形式為“ null”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM