我正在用 java 为一个项目创建家谱,但我不知道如何设置树的根值
public class FamilyTree {
class Node {
String value;
Node left, right;
Node(String value) {
this.value = value;
right = null;
left = null;
}
}
Node top;
public static void main(String[] args) {
FamilyTree f = new FamilyTree();
f.top.value = "Steve"; //parent node
f.add(f.top, "Steve", "Mary");
f.add(f.top, "Steve", "Sue");
}
}
它只是显示错误“线程“主”java.lang.NullPointerException 中的异常:无法分配字段“值”,因为“f.top”为空”