0

我是 Java 新手,我正在做关于构建决策树的作业。经过2天的不断编码,我终于建树并手动验证。但是我坚持验证树,因为每次我尝试将“节点”对象传递给 Validator 类时,它都是空的。我尝试了各种以前的建议,但似乎没有任何效果。我需要有人指出我的错误以及为什么它是错误的。这是一小部分代码,将解释我想要实现的目标。请就我应该如何处理这个问题提出建议。

//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();
                }
       }
4

3 回答 3

0

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

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

于 2012-10-14T01:15:38.793 回答
0

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

this.bTree = bTree;

添加行

bTree.buildDecisionTree();

您的代码中没有这一行,因此bTree.node默认为 null,因为它没有被初始化。这this.node在该行之后变为空

this.node = bTree.node;

导致null pointer exception您稍后尝试引用this.node。通过此更改,您的代码应该可以工作。如果您有任何问题,请告诉我。

于 2012-10-14T01:01:07.530 回答
0

您的节点为空,因为它的方法签名buildDecisionTree()Object或者其他未指定的对象;方法签名void甚至不会编译。

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

于 2012-10-14T00:09:09.750 回答