Java 在链表上迭代时出现NullPointerException

Java 在链表上迭代时出现NullPointerException,java,nullpointerexception,linked-list,Java,Nullpointerexception,Linked List,代码背后的概念是删除列表中大于前面元素的元素。在这种情况下,我的节点有一个int数据,并且可以通过该数据进行比较。(这些课程都是可比的。) 问题在于,当此代码与链接列表一起运行时,我会收到nullpointexception: [2,5,4,3,7,6,4,2,3,4,5] 应获取的预期列表为 [2,2] 因为(5>2)删除5,然后(4>2)删除4,然后(3>2)删除3…依此类推,直到它以nullpointerexception结束 另一个例子是清单 [3,1,-2,3,6,-1,3,2

代码背后的概念是删除列表中大于前面元素的元素。在这种情况下,我的节点有一个int数据,并且可以通过该数据进行比较。(这些课程都是可比的。)

问题在于,当此代码与链接列表一起运行时,我会收到nullpointexception:

 [2,5,4,3,7,6,4,2,3,4,5] 
应获取的预期列表为

[2,2]
因为(5>2)删除5,然后(4>2)删除4,然后(3>2)删除3…依此类推,直到它以nullpointerexception结束

另一个例子是清单

[3,1,-2,3,6,-1,3,2,1]
这份名单最终应该是

[3,1,-2]
其中的调试代码用于显示删除了哪些元素

getter方法是最基本的,并且工作良好

public void deleteIncrementing() {
    T largest = null;

    while(head.getNext() != null || head != null) {
        Node<T> temp = head.getNext();

        while(temp.getValue().compareTo(head.getValue()) > 0){
            largest = temp.getValue();
            remove(largest);
            System.out.println(largest); // debug
            if(temp.getNext() == null){
                break;
            }
            temp = head.getNext();
        }
        head = temp;
    }
}
public void deleteIncrementing(){
T最大=零;
while(head.getNext()!=null | | head!=null){
节点温度=head.getNext();
而(temp.getValue().compareTo(head.getValue())>0){
最大=临时getValue();
移除(最大);
System.out.println(最大);//调试
if(temp.getNext()==null){
打破
}
temp=head.getNext();
}
压头=温度;
}
}
源自建议的伪代码:

    Node<T> current = head;
    Node<T> previous = null;

    while(current != null) {
        if (previous != null){
            if (current.getValue().compareTo(previous.getValue()) > 0){
                //System.out.println(current.getValue().toString());
                remove(current.getValue());
            }
            if (current.getValue().compareTo(previous.getValue()) < 0){
                //System.out.println(previous.getPrevious().getValue().toString());
                //System.out.println(current.getValue().toString());
                remove(previous.getValue());
            }
        }

        previous = current;
        current  = current.getNext(); 
    }
节点电流=头部;
Node-previous=null;
while(当前!=null){
如果(上一个!=null){
如果(当前.getValue().compareTo(先前的.getValue())>0){
//System.out.println(current.getValue().toString());
删除(current.getValue());
}
如果(当前.getValue().compareTo(先前的.getValue())<0){
//System.out.println(previous.getPrevious().getValue().toString());
//System.out.println(current.getValue().toString());
删除(previous.getValue());
}
}
先前=当前;
current=current.getNext();
}

这仍然是不正确的,因为它没有考虑从第一个到最后一个元素,并保留最后一个元素…有什么原因吗?

对于初学者,此条件:

while (head.getNext() != null || head != null)
应该是:

while (head != null && head.getNext() != null)

始终先检查空值!

我不知道这是否是您出现异常的原因,但您必须在while循环中切换测试:

while (head != null && head.getNext() != null)
当您第一次测试
head.getNext()!=null
并且head为null时,将抛出NullPointerException


你对这个问题想得太多了

考虑您需要完成的工作:编写一个函数,删除其值大于前一个元素值的LinkedList元素

阅读此语句后,您应该了解在链接点(如当前元素和前一个元素)的每个点上需要考虑的内容。因此,不需要嵌套的while循环。

伪代码:

current  = head 
previous = null

while current is not null
   is previous non-null and current > previous?
      if yes: delete current
      if  no: do nothing

   previous = current
   current  = current.next 

看看您是否可以使用此伪代码简化您自己的代码。

除了其他人提到的对
头的错误检查之外,我认为这可能存在一些问题:

    while(temp.getValue().compareTo(head.getValue()) > 0){
        largest = temp.getValue();
        remove(largest);
        System.out.println(largest); // debug
        if(temp.getNext() == null){
            break;
        }
        temp = head.getNext();
    }
    head = temp;
temp
将引用即将被删除的节点。该节点会发生什么情况?我不知道,因为我不知道这是什么列表类型,也不知道
remove
的作用。但是,除非list类的文档明确说明
remove
不会影响节点的“下一步”指针,我不会指望它。一般来说,这种数据应该在
remove
调用之前保存。(在其他语言中,如果有一个“remove”也显式释放存储,那么肯定需要先保存链接。)


更严重的问题是,即使“next”指针保持不变,列表末尾会发生什么。
temp
指向一个节点,然后该节点被删除。它的“next”指针为
null
,因此您会中断。但随后您设置了
head=temp
,这意味着
head
现在指向一个不再在列表中的节点。我认为您只希望
head=temp
是因为
比较而到达该点,而不是因为到达列表末尾而到达该点。

对java代码进行了修改,并将其添加到主要问题中-仍然是空指针异常。但这样做更有意义。感谢您,因为您执行空检查不正确,有其他方法检查空值…?或者您的意思是将其更改为previous!=null?这反过来不会产生写输出,但更接近。t谢谢。问题不在于previous.getValue()为空,而在于previous本身为空。我想我改变了它,但它仍然不正确。在过去的30分钟里,我一直在试图弄清楚为什么它不能解释这两个元素?(用新代码更新了问题)我猜您应该删除
|
并在下面的
行中添加
&&
,而(head.getNext()!=null | | head!=null)