I'm new to Java and I am doing a homework on constructing Decision Trees. After 2 days of continuous coding, I finally built the tree and verified it manually. But I'm stuck on validating the tree, because, everytime I try to pass the "node" object to the Validator class, it is null. I tried all sorts of previous suggestions and nothing seems to work. I need someone to point out my mistake and why its a mistake. Here is a short portion of the code that will explain what I'm trying to achieve. Please advice on how I should go about this.
//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();
}
}
Your node is null because the method signature for buildDecisionTree()
is either Object
or some other unspecified object; the method signature with void
won't even compile.
You should change your method there to return an object of type DecisionTreeNode
.
Your problem is that you are not initializing bTree.node
properly. In your DecisionTreeValidator
constructor, directly after the line
this.bTree = bTree;
add the line
bTree.buildDecisionTree();
You did not have this line in your code, and thus bTree.node
was defaulted to null as it wasn't initialized. This made this.node
null after the line
this.node = bTree.node;
resulting in a null pointer exception
when you tried to reference this.node
later on. With this change, your code should work. Tell me if you have any problems.
I figured out what the issue was. It wasnt related to the node being null. the node wasnt null. when i traverse the tree and hit the leaf node, I was still checking for branches. corrected it now. Its working perfect. Thank you all for your recommendations. It helped me learn.
I will now remove the "logic" for tree building.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.