[英]All elements in arrayList is null when add to arrayList in java
I have created a List call nodes_ and initialized as a ArrayList, when I add something to nodes_ , all is null. 当我向nodes_添加内容时,我创建了一个List调用nodes_并初始化为ArrayList,所有均为null。 There is a piece of code :
有一段代码:
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);
and there is another piece of other class: 还有另外一类:
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);
}
When i debuged , it looks like this : debug image 当我调试时,它看起来像这样: debug image
What you're seeing ( "null"
) is the output of toString()
called on each of those classes. 您看到的(
"null"
)是在每个这些类上调用的toString()
的输出。 They aren't actually null. 它们实际上不是空的。
How can you tell? 你怎么知道? The debugger shows each entry like
ClassNode@464
. 调试器显示每个条目,例如
ClassNode@464
。 That refers to a specific instance, not null. 那是指一个特定的实例,而不是null。
The nodes are not null. 节点不为空。 The objects are all there, but their toString representation is "null".
对象全部存在,但是它们的toString表示形式为“ null”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.