尝试更改字符串树中根节点的值(java)

问题描述 投票:0回答:0

我正在用 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”为空”

java string tree root-node
© www.soinside.com 2019 - 2024. All rights reserved.