0

Im working on a right rotate method for when i splay. I have nodes that i use to hold the value of the left child and the left childs right. On the case that im testing the left childs right is null but when i compile i get a null pointer exception. Even if the value is null shouldnt it just assign the new node to null rather than throw a null pointer exception?

below are my assignments

Node<E> leftC = x.getLeft();
Node<E> leftsRight = leftC.getRight();

in the case im testing, the tree looks like

     3
    /
   2
  /
 1

and x is 3, so then leftC is 2 and leftsRight should be null but it just throws the null pointer.

Why is it throwing the null pointer instead of just assigning it to null?

user214577
  • 363
  • 1
  • 7
  • 15
  • The most likely thing is that `x.getLeft()` is returning `null`, not 2. Then when you try to evaluate `leftC.getRight()`, `leftC` is `null`, which is causing the NPE. You can verify this in the debugger. – Ted Hopp Mar 07 '13 at 04:20
  • 1
    Assuming that the NullPointerException originates from one of the two lines you posted (verify it), then it means that either x is null, of leftC is null. There's no other option. – Eyal Schneider Mar 07 '13 at 04:45

0 Answers0