[英]Passing objects between classes in Java
I'm new to Java and I am doing a homework on constructing Decision Trees. 我是Java的新手,我正在做有关构造决策树的作业。 After 2 days of continuous coding, I finally built the tree and verified it manually.
经过2天的连续编码,我终于构建了树并手动进行了验证。 But I'm stuck on validating the tree, because, everytime I try to pass the "node" object to the Validator class, it is null.
但是我一直坚持验证树,因为每次尝试将“节点”对象传递给Validator类时,它都是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; 您的节点为null,因为
buildDecisionTree()
的方法签名是Object
或其他一些未指定的对象。 the method signature with void
won't even compile. 带
void
的方法签名甚至不会编译。
You should change your method there to return an object of type DecisionTreeNode
. 您应该在那里更改方法以返回
DecisionTreeNode
类型的对象。
Your problem is that you are not initializing bTree.node
properly. 您的问题是您没有正确初始化
bTree.node
。 In your DecisionTreeValidator
constructor, directly after the line 在您的
DecisionTreeValidator
构造函数中,直接在该行之后
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. 您的代码中没有此行,因此
bTree.node
默认为null,因为它尚未初始化。 This made this.node
null after the line 这使得
this.node
之后为null
this.node = bTree.node;
resulting in a null pointer exception
when you tried to reference this.node
later on. 当您稍后尝试引用
this.node
时,导致null pointer exception
。 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.
该节点不是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. 我现在将删除用于构建树的“逻辑”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.