0

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

3 に答える 3

0

のメソッドシグネチャbuildDecisionTree()がいずれかObjectまたはその他の指定されていないオブジェクトであるため、ノードはnullです。のメソッドシグネチャはvoidコンパイルすらしません。

タイプのオブジェクトを返すには、そこでメソッドを変更する必要がありますDecisionTreeNode

于 2012-10-14T00:09:09.750 に答える
0

問題は、bTree.node適切に初期化していないことです。コンストラクターで、DecisionTreeValidator行の直後

this.bTree = bTree;

行を追加します

bTree.buildDecisionTree();

コードにこの行が含まれていなかったため、bTree.node初期化されなかったため、デフォルトでnullになりました。これthis.nodeは行の後にnullになりました

this.node = bTree.node;

後でnull pointer exception参照しようとすると、が発生します。this.nodeこの変更により、コードは機能するはずです。何か問題があれば教えてください。

于 2012-10-14T01:01:07.530 に答える
0

私は問題が何であるかを理解しました。ノードがnullであることに関連していませんでした。ノードはnullではありませんでした。木を横断して葉のノードにぶつかったとき、私はまだ枝をチェックしていました。今それを修正しました。その完璧な動作。お勧めありがとうございます。それは私が学ぶのを助けました。

次に、ツリー構築の「ロジック」を削除します。

于 2012-10-14T01:15:38.793 に答える