java - Java 中的 'Null pointer access' 有问题

标签 java nullpointerexception

if(key == '1'){//insert at ->right.right
    BinaryNode tempPointer = root;

    while(tempPointer != null){
        tempPointer = tempPointer.right;
    }
    BinaryNode newNode = new BinaryNode(x);
    newNode.right = null;
    newNode.left = null;
    size++;
    lastNode = newNode;
    newNode.parent = tempPointer;
    tempPointer.right = newNode;
}

它一直说 termPointer 只能在这个位置为空。虽然我不明白为什么。

这也失败了:

newNode.parent = tempPointer.parent; //'tempPointer can only be null here'
tempPointer = newNode;

最佳答案

您的 while 循环只会在 tempPointer 为 null 时结束。在循环之后,您没有将 tempPointer 设置为任何其他值,因此它将保持 null 直到函数结束。

关于java - Java 中的 'Null pointer access' 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5529131/

相关文章:

java - Android Webview 因错误而崩溃(错误 | EGL_BAD_DISPLAY)

java - 在方法定义中使用 @deprecated 并更改参数类型

java - 如何从另一个 netbeans 模块更新 jTable

deployment - 部署 webLogic 12c 应用程序时出现 nullPointerException

java - 在 Java 中使用 Rserve 时出现 ClassNotFoundException

java - 在chrome最新版本中使用selenium处理打印预览窗口

java - JComboBox:希望第一个条目是空白条目

java - 非 Activity 类的Android通知

android - Xmpp String utils 经常使应用程序崩溃

com.mysql.jdbc.ConnectionImpl.getServerCharset 处的 java.lang.NullPointerException(ConnectionImpl.java :3005)