title: NullPointerException when object is initialized in another method
body: <p>I was trying to learn about binary trees and <code>NullPointerException</code>were coming. So I decided to write a little program to try to understand it. Here is the code:</p>
  
  <pre><code>public class Nulls
  {
      static Node node;
      private class Node
      {
          int val;
          Node right;
          Node left;
      }
      public void init(Node n)
      {
          if(n==null)n=new Node();
      }
      public void print()
      {
           System.out.println(node);
      }
      public static void main(String[] args)
      {
          Nulls n=new Nulls();
          n.init(node);
          n.print();
      }
  }
  </code></pre>
  
  <p>The output was <code>null</code>. If I understand properly, the node object was initialized and the output should be the object's <code>toString()</code> method. And as the <code>print()</code> method is executed after <code>init(Node n)</code>, the output should not be <code>null</code>. What is the problem?</p>