开发者社区> 问答> 正文

在插入LinkedList时抛出NullPointerException时进行Null检查

因此,我使用Java进行编码,因此必须手动创建LinkedList。它是双重链接的,尾部的下一个指针指向null。我正在使用它来遍历列表,直到到达排序算法(气泡排序)的末尾为止。

Node<?> current = a.getHead();
while (current.getNext() != null) { //this line throw a NullPointerException
         //sorting algorithm
        current = current.getNext();
}

下面是GETNEXT(),以及代码: Node<?> current = a.getHead();。为什么Java在这里抛出NullPointerException?

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 17:01:50 632 0
1 条回答
写回答
取消 提交回答
  • 问题出在 Node<?> current = a.getHead();

    a.getHead(); is returning null.
    

    请检查像-

    while (current != null && current.getNext() != null) {
             //sorting algorithm
            current = current.getNext();
    }
    

    回答来源:Stack Overflow

    2020-03-27 17:02:21
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载