This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我已经看过很多关于这个用尽的问题的问题,但是我找不到为什么我在检查null时会得到NPE的答案。我自己和另一个伙伴仅通过使用旋转就可以构建自平衡二进制搜索,因此,如果新的根目录中有一个需要放置在另一侧的子节点,则我们需要存储一个临时节点。

在这里,我们检查nextRoots左或右子级是否为null,如果不是,我们将其存储在临时节点中以备将来放置。

            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;
            }


我的印象是,if检查应该是解决此问题的方法,但它本身就是导致问题的原因。任何帮助将不胜感激,如果您需要更多代码,请告诉我。

最佳答案

如果nextRoot.leftChild抛出NPE,则nextRoot为null。
尝试检查行为代码是否正确。
如果是...修改以下内容:

if (nextRoot != null && nextRoot.leftChild != null)

关于java - 为什么在if!= null检查中得到NullPointerException ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15593766/

10-09 18:28
查看更多