-2

I have following if statement:

if (node.left.left == null && node.left.right != null)

Related to a binary tree. Before this if statement I have a other if statement that checks if node.left is null.

Why am I getting null pointer exception on this statement?

Full code in the method up until the exception:

 BinaryTreeNode node= root;


if (root==null)
    return true;
else
    {
        while(node!=null)
        {
        if (node.key>key && node.left !=null)
        {
            if (node.left.key==key)
                {
                    if(node.left.left==null && node.left.right==null)
                        node.left=null;
                    if(node.left.left==null && node.left.right!=null)
                        node.left=node.left.right;
Dole
  • 339
  • 4
  • 16

1 Answers1

4
                    node.left=null;
                if(node.left.left==null && node.left.right!=null)
                    node.left=node.left.right

You are explicitly setting node.left to null.

D M
  • 1,410
  • 1
  • 8
  • 12