Java 使用findChild方法构建文件树

Java 使用findChild方法构建文件树,java,nullpointerexception,Java,Nullpointerexception,我想实现以下代码。要求如下 完成构造函数并测试在main方法中所做的工作 在方法main中,构建一个包含几个节点的树,然后为每个名称调用findChild。他们将失败。完成方法findChild,直到测试通过 我感到困惑的一点是f1的FileNode为null,并且不能将null值放入hashmap(将显示null指针错误)。我应该如何防止这个问题 这是我做的代码 package part2; import java.util.Map; import javax.swing.JFileCho

我想实现以下代码。要求如下

  • 完成构造函数并测试在main方法中所做的工作
  • 在方法main中,构建一个包含几个节点的树,然后为每个名称调用findChild。他们将失败。完成方法findChild,直到测试通过 我感到困惑的一点是f1的FileNode为null,并且不能将null值放入hashmap(将显示null指针错误)。我应该如何防止这个问题

    这是我做的代码

    package part2;
    
    import java.util.Map;
    
    import javax.swing.JFileChooser;
    
    import java.io.File;
    import java.util.Collection;
    
    
    /**
     * The root of a tree representing a directory structure.
     */
    public class FileNode {
    
        /** The name of the file or directory this node represents. */
        private String name;
        /** Whether this node represents a file or a directory. */
        private FileType type;
        /** This node's parent. */
        private FileNode parent;
        /**
         * This node's children, mapped from the file names to the nodes. If type is
         * FileType.FILE, this is null.
         */
        private Map<String, FileNode> children;
    
        /**
         * A node in this tree.
         *
         * @param name
         *            the file
         * @param parent
         *            the parent node.
         * @param type
         *            file or directory
         * @see buildFileTree
         */
        public FileNode(String name, FileNode parent, FileType type) {
            this.name = name;
            this.parent = parent;
            this.type = type;
    
    
    
            // TODO: complete this method.
        }
    
        /**
         * Find and return a child node named name in this directory tree, or null
         * if there is no such child node.
         *
         * @param name
         *            the file name to search for
         * @return the node named name
         */
        public FileNode findChild(String name) {
            // TODO: complete this method.
                if (children.containsKey(name)) {
                    return children.get(name);
                } 
                else{
                    return null;
                }
    
        }
    
        /**
         * Return the name of the file or directory represented by this node.
         *
         * @return name of this Node
         */
        public String getName() {
            return this.name;
        }
    
        /**
         * Set the name of the current node
         *
         * @param name
         *            of the file/directory
         */
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * Return the child nodes of this node.
         *
         * @return the child nodes directly underneath this node.
         */
        public Collection<FileNode> getChildren() {
            return this.children.values();
        }
    
        /**
         * Return this node's parent.
         * 
         * @return the parent
         */
        public FileNode getParent() {
            return parent;
        }
    
        /**
         * Set this node's parent to p.
         * 
         * @param p
         *            the parent to set
         */
        public void setParent(FileNode p) {
            this.parent = p;
        }
    
        /**
         * Add childNode, representing a file or directory named name, as a child of
         * this node.
         * 
         * @param name
         *            the name of the file or directory
         * @param childNode
         *            the node to add as a child
         */
        public void addChild(String name, FileNode childNode) {
            this.children.put(name, childNode);
        }
    
        /**
         * Return whether this node represents a directory.
         * 
         * @return whether this node represents a directory.
         */
        public boolean isDirectory() {
            return this.type == FileType.DIRECTORY;
        }
    
        /**
         * This method is for code that tests this class.
         * 
         * @param args
         *            the command line args.
         */
        public static void main(String[] args) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int returnVal = fileChooser.showOpenDialog(null);
            File file;
    
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
                System.out.println(file);
            }
            System.out.println("Testing FileNode");
            FileNode f1 = new FileNode("top", null, FileType.FILE);
            FileNode f2 = new FileNode("top", f1, FileType.FILE);
    
            f2 = file;
            f1.addChild("c1", f2);
    
            System.out.println(f2.findChild("c1"));
            if (!f1.getName().equals("top")) {
                System.out.println("Error: " + f1.getName() + " should be " + "top");
            }
    
    
        }
    
    }
    
    f1的FileNode为null,不能将null值放入hashmap中

    我认为null可以放在HashMap中,至少作为值

    该错误意味着
    children==null
    ,因为您从未初始化过它!所以请修正

    public void addChild(String name, FileNode childNode) {
        if (this.children == null) {
            this.children = new HashMap<String, FileNode>();
        }
        this.children.put(name, childNode);
    }
    

    您需要在何处将null放入HashMap
    this.name=name;this.parent=parent这有什么问题?将空的父级解释为“没有父级,因此必须在根目录中”,因此您可以搜索“/”的文件系统条目,因为您似乎没有尝试就有了
    TODO
    @AdrianColomitchi,是的,我知道,但是如何将文件转换为FileNode。FileNode应该是FileNode。它的可能副本仍然有错误。eclipse说错误发生在这里:this.children.put(name,childNode);然后
    this.children==null
    ,可能是我在answer@J.DOE请不要使用答案中的代码编辑您的问题。它使答案无效,并使你的问题对未来的读者来说不清楚。我要回去了。您应该添加错误的stacktrace。好吧,错误说明hashmap无法添加特定的子节点。我也更新了我的代码,你能帮我解决这个问题吗?
    public void addChild(String name, FileNode childNode) {
        if (this.children == null) {
            this.children = new HashMap<String, FileNode>();
        }
        this.children.put(name, childNode);
    }
    
    public FileNode findChild(String name) {
        // Base case - no children
        if (children == null) return null;
    
        // Get the child, if exists
        FileNode immediateChild = children.get(name);
        if (immediateChild != null) {
            return children.get(name);
        } 
        else { // Iterate all children until found
            FileNode tmp = null;
            for (FileNode f : getChildren()) {
                if (tmp != null) break;
                tmp = f.getChild(name);
            }
            return tmp;
        }
    }