简体   繁体   English

交互 LinkedList 时的空检查抛出 NullPointerException

[英]Null Check when interating LinkedList throws NullPointerException

So I'm coding in Java, and I had to make a LinkedList manually.所以我用 Java 编码,我不得不手动创建一个 LinkedList。 It is doubly linked, and the tail's next pointer points to null.它是双向链接的,尾部的next指针指向null。 I'm using this to iterate through the list until I reach the end for a sorting algorithm (bubble sort).我正在使用它来遍历列表,直到到达排序算法(冒泡排序)的末尾。

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

Here's the code for getNext() as well: Node<?> current = a.getHead();下面是 getNext() 的代码: Node<?> current = a.getHead(); . . Why is Java throwing a NullPointerException here?为什么 Java 在这里抛出 NullPointerException 异常?

Problem is in line Node<?> current = a.getHead();问题出在Node<?> current = a.getHead();

a.getHead(); is returning null.

Please check like -请检查像 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM