java - java中添加到arrayList时,arrayList中的所有元素均为null

标签 java arraylist nullreferenceexception

我创建了一个List调用nodes_并初始化为ArrayList, 当我向nodes_添加一些东西时,一切都是空的。 有一段代码:

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。它指的是一个特定的实例,不为空。

关于java - java中添加到arrayList时,arrayList中的所有元素均为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35071342/

相关文章:

nullreferenceexception - 如何修复 Orchard CMS 中的 "Object reference not set to an instance of an object"错误

asp.net - 静态方法中的 HttpContext.Current NullReferenceException

c# Xunit,Assert.Throws 在引发预期异常时失败

java - 将 WAR 部署到 Tomcat(Spring Boot + Angular)

Java - 列表组合

java - 我有一个 JTextField 引用列表。有没有办法通过数组来初始化它们?

java - 如何找到3个数组中共有的最小数字?

java - 如何在没有认证问题的情况下连接到 URL?

java - 如果 JNLP url 包含 %,为什么 Java WebStart 应用程序拒绝启动?

java - 死锁预防(Java+MySQL)