Why am I getting a NullPointerException in an if != null check <p>I have looked at many questions regarding this worn out question but I cannot find an answer for why I would be getting a NPE during my check for null. Myself and another partner are building a self-balancing binary search simply by using rotation so we need to store a temp Node if the new root has a child that needs to get placed on the opposite side.</p> <p>Here we check if the nextRoots left or right child is null and if not we store it in a temp Node for future placement.</p> <pre><code> Node temp = null; nextRoot = root.rightChild; // Set the next root oldRoot = root; // Hold the old root // If next roots left child is NOT null // Lets store it and null it now if (nextRoot.leftChild != null) // This check throws NPE, not nextRoot, just the nextRoot.leftChild { temp = nextRoot.leftChild; nextRoot.leftChild = null; } </code></pre> <p>I am under the impression that the if check should be the way to circumvent this problem but it in itself is causing the problem. Any help would be greatly appreciated and if you need more code please let me know.</p>