繁体   English   中英

在Java中的类之间传递对象

[英]Passing objects between classes in Java

我是Java的新手,我正在做有关构造决策树的作业。 经过2天的连续编码,我终于构建了树并手动进行了验证。 但是我一直坚持验证树,因为每次尝试将“节点”对象传递给Validator类时,它都是null。 我尝试了以前的建议并没有什么种种似乎工作。 我需要有人指出我的错误以及为什么是错误。 这是代码的一小部分,它将解释我要实现的目标。 请提出建议,我应该怎么做。

//Node Class to represent a node in the tree
public class DecisionTreeNode 
{
    String attribute;
    boolean isLeaf;
    DecisionTreeBranch[] branches; //Another class to represent branch from a node

    //Default constructor for a Node: With attributes, label and isLeaf condition
    public DecisionTreeNode(String attribute) 
    {
        this.attribute = attribute;
        this.isLeaf = true;
    }
        ............
}

//Tree class with logic to build the tree
public class BuildDecisionTree 
{
    public PrepareFile config; //Need this object to get a arraylist of values to construct the tree
    DecisionTreeNode root;
        BuildDecisionTree(PrepareFile config)
    {
        this.config = config;
    }
    //Construct Decision Tree
    public void buildDecisionTree() 
    {
        root = myDecisionTreeAlgorithm(config.getExamples(), config.getAttributes());
        System.out.println("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Decision tree was constructed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
        root.printDecisionTree("");
    }

//This is the validator where is want both the "config" and the "root" objects

import java.util.List;

public class DecisionTreeValidator 
{
    PrepareFile config;
    DecisionTreeNode node;
    BuildDecisionTree bTree;    
    public DecisionTreeValidator(BuildDecisionTree bTree, PrepareFile config)
    {
        this.bTree = bTree;
        this.node = bTree.root; //I tried adding a getter function in BuildDecisionTree class and returned the root, even then this was null. Like below
            //this.node = bTree.buildDecisionTree(); //made the return type of the buildDecisionTree function as "DecisionTreeNode"
        this.config = config;
        this.examples = config.getExamples();
    }
    public boolean validateSingleExample(Example example)
    {

        boolean result = true;
        while(node.isLeaf == false) //THIS IS WHERE I GET THE NULL POINTER EXCEPTION
                ...........................
    }
}

//Main class
       public class PredictRestaurant 
        {
            public static void main(String[] args) 
            {
                PrepareFile config = new PrepareFile();
                BuildDecisionTree bTree = new BuildDecisionTree(config);
                DecisionTreeValidator validator = new DecisionTreeValidator(bTree, config);
                boolean isTrain = true;
                        config.setTreeParameters();
                        bTree.buildDecisionTree();
                }
       }

您的节点为null,因为buildDecisionTree()的方法签名是Object或其他一些未指定的对象。 void的方法签名甚至不会编译。

您应该在那里更改方法以返回DecisionTreeNode类型的对象。

您的问题是您没有正确初始化bTree.node 在您的DecisionTreeValidator构造函数中,直接在该行之后

this.bTree = bTree;

添加线

bTree.buildDecisionTree();

您的代码中没有此行,因此bTree.node默认为null,因为它尚未初始化。 这使得this.node之后为null

this.node = bTree.node;

当您稍后尝试引用this.node时,导致null pointer exception 进行此更改后,您的代码应该可以工作。 告诉我您是否有任何问题。

我弄清楚了问题所在。 它与节点为空无关。 该节点不是null。 当我遍历树并击中叶子节点时,我仍在检查分支。 现在纠正它。 它的工作完美。 谢谢大家的建议。 它帮助我学习。

我现在将删除用于构建树的“逻辑”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM