1

in Line 7 i get nullpointerexception, how can i fix that ? any ideas?

private void updateHeights(BSTreeNode v) {
  BSTreeNode u = v;

  while (u != null) {
    int bfc=updateNodeHeight(u);
    u = u.parent;

    if (bfc<=-2) {

      if( getHeight(u.left.left) >= getHeight(u.left.right) ) { // Null Pointer Exception
        u = rotateRight(u);

      } else {
        u=LR(u);
      }
    }
  }
}   
ThisaruG
  • 3,222
  • 7
  • 38
  • 60

1 Answers1

1

There are two possibel problems, and as many solutions. First is placing a null check in getHeight() which I assume has been done based on your programming style (the check in while conditional). That leaves a possible null pointer issue with u.left. As such:

Ensure u.left is not null, like this:

getHeight(u.left != null && ((u.left.left)>=getHeight(u.left.right))
Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/10660885) – Luca Detomi Dec 23 '15 at 07:48
  • There are two possibel problems, and ways to solve them. First is placing a null check in `getHeight()` which I *assume* has been done based on the check in while conditional. That leaves a possible null pointer issue with `u.left`. As such, I posted this as an answer. – Pétur Ingi Egilsson Dec 23 '15 at 14:06
  • I have nothing against your answer, simply SO generally requires a detailed one. This is the reason of my flag in "review" mode. I suggest you to better exlain "why" your answer could be the solution, to make it a perfect one. ;-) – Luca Detomi Dec 23 '15 at 14:10
  • 1
    I apreciate the feedback, I have updated my answer! Marry christmas :) – Pétur Ingi Egilsson Dec 23 '15 at 14:16
  • 1
    Now it's ok! Great and merry christmas you too – Luca Detomi Dec 23 '15 at 14:17