I was trying to learn about binary trees and NullPointerExceptionwere coming. So I decided to write a little program to try to understand it. Here is the 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();
}
}
The output was null. If I understand properly, the node object was initialized and the output should be the object's toString() method. And as the print() method is executed after init(Node n), the output should not be null. What is the problem?