title: Passing objects between classes in Java
body: <p>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.</p>
  
  <pre><code>//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();
                  }
         }
  </code></pre>