开发者社区 问答 正文

在插入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 691 分享 版权
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
    赞同 展开评论
问答分类:
问答地址: