title: Binary Trees and NullPointerException
body: <p>This method takes a tree of integers and constructs a String of the following:</p>
  
  <p>(The data at the root, String of left subtree, Stringof right subtree)</p>
  
  <p>For example, if a variable tree stores a reference to the following tree:</p>
  
  <pre><code>          +---+
            | 2 |
            +---+
           /     \
       +---+     +---+
       | 8 |     | 1 |
       +---+     +---+
      /         /     \
  +---+     +---+     +---+
  | 0 |     | 7 |     | 6 |
  +---+     +---+     +---+
           /               \
       +---+               +---+
       | 4 |               | 9 |
       +---+               +---+
  </code></pre>
  
  <p>It should return a String of:</p>
  
  <pre><code>"(2, (8, 0, empty), (1, (7, 4, empty), (6, empty, 9)))"
  </code></pre>
  
  <p>The method should return "empty" for an empty tree. For a leaf node, it should return the data in the node as a String. For a branch node, it should return a parenthesized String that has three elements separated by commas: </p>
  
  <p>With my method, it sometimes works, but sometimes, it generates a NullPointerException. Can anyone point out where and why this is happening?</p>
  
  <p>Here is what I have:</p>
  
  <pre><code> private IntTreeNode overallRoot; // first node; linked to other nodes
  
   // post: returns the string of all data in leaves. For a branch node, return a parenthesized String
   //       w/ three elementsseparated by commas. return empty for empty tree
   public String toString2() {
       if (overallRoot == null) { // If empty tree
           return "empty";
       } else {
           return toString2(overallRoot);
       }
   }
  
   // helper for toString
   private String toString2(IntTreeNode root) {
       String value = "";
       if (root.left != null &amp;&amp; root.right != null) { // If both branches exist
           value += "(" + root.data + ", " + toString2(root.left)+ ", " + toString2(root.right) + ")";  
       } else if (root.left != null &amp;&amp; root.right == null) { // if right branch is empty
           value += "(" + root.data + ", " + toString2(root.left) + ", empty)";
       } else if (root.left == null &amp;&amp; root.right != null) { // if left branch is empty
           value += "(" + root.data + ", empty, " + toString2(root.left) + ")";
       } else { // If at a leaf
           return "" + root.data;
       }
       return value;
   }
  </code></pre>
  
  <p>Here is the node class:</p>
  
  <pre><code>public class IntTreeNode {
       public int data;
       public IntTreeNode left;
       public IntTreeNode right;
  
       // constructs a leaf node with given data
       public IntTreeNode(int data) {
           this(data, null, null);
       }
  
       // constructs a branch node with given data, left subtree,
       // right subtree
       public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {
           this.data = data;
           this.left = left;
           this.right = right;
       }
  }
  </code></pre>