在Java中的类之间传递对象

时间:2012-10-14 00:06:11

标签: java class object

我是Java的新手,我正在做构建决策树的功课。经过2天的连续编码,我终于构建了树并手动验证了它。但我坚持验证树,因为每次我尝试将“node”对象传递给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();
                }
       }

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

之后
this.bTree = bTree;

添加行

bTree.buildDecisionTree();

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

之后为空
this.node = bTree.node;
当您稍后尝试引用null pointer exception时,

会产生this.node。 通过此更改,您的代码应该可以运行。如果您有任何问题,请告诉我。

答案 2 :(得分:0)

我弄清楚问题是什么。它与节点为空无关。节点不是null。 当我遍历树并击中叶节点时,我仍然在检查树枝。现在纠正了。它的工作完美。谢谢大家的推荐。它帮助我学习。

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