尝试在Java中创建双端队列时出现Nullpointer异常[重复]

时间:2022-06-11 20:34:54

This question already has an answer here:

这个问题在这里已有答案:

I have been trying to create a deque in Java, but I get a nullpointer exception when I add a new node. Here is the helper class I use for the doubly-linked list:

我一直在尝试用Java创建一个双端队列,但是当我添加一个新节点时,我得到了一个nullpointer异常。这是我用于双向链表的辅助类:

private class Node {
    private Item item;
    private Node next;
    private Node prev;
}

Then I add nodes to the beginning of the list as follows:

然后我将节点添加到列表的开头,如下所示:

    public void addFirst(Item item) {
    Node oldfirst = first;
    first = new Node();      
    first.prev = null;
    first.item = item;
    first.next = oldfirst;
    oldfirst.prev = first;        
    N++;
}

I get a NullPointer exception for the oldfirst.prev = first; line. Also, when I compile the code I get the following warning:

我得到了oldfirst.prev = first的NullPointer异常;线。此外,当我编译代码时,我收到以下警告:

Warning: The field Deque2<Item>.Node.prev is never read locally

警告:永远不会在本地读取字段Deque2 .Node.prev

What might I be doing wrong?

我可能做错了什么?

2 个解决方案

#1


0  

The exception means that oldfirst is null. And you initialise it as Node oldfirst = first, so it means that first is null.

该异常意味着oldfirst为null。并将它初始化为Node oldfirst = first,这意味着first首先为null。

#2


0  

My guess is that when your deque is empty, first is null. Then when you assign first into oldFirst, that makes oldFirst null, and so trying to access prev is giving an NPE. Changing the access to:

我的猜测是当你的双端队列是空的时候,首先是空的。然后当你首先分配到oldFirst时,这会使oldFirst为null,因此尝试访问prev会给出一个NPE。将访问权限更改为:

if (oldFirst != null)
    oldfirst.prev = first;

should fix it.

应该解决它。

Your warning is probably just because you're not atcually using prev yet in your deque, you're just setting it. Fun fact: with a deque, you don't need a doubly-linked list, so your prev variable is unlikely to ever be used. If you also want to support queuing with your deque down the road (similar to how LinkedList implements both Deque and Queue) then you'll need to keep prev.

你的警告可能只是因为你在骗局中没有使用prev,你只是设置它。有趣的事实:使用双端队列,您不需要双向链表,因此不太可能使用您的prev变量。如果你还想支持你的deque在路上排队(类似于LinkedList如何实现Deque和Queue),那么你需要保持流行。

#1


0  

The exception means that oldfirst is null. And you initialise it as Node oldfirst = first, so it means that first is null.

该异常意味着oldfirst为null。并将它初始化为Node oldfirst = first,这意味着first首先为null。

#2


0  

My guess is that when your deque is empty, first is null. Then when you assign first into oldFirst, that makes oldFirst null, and so trying to access prev is giving an NPE. Changing the access to:

我的猜测是当你的双端队列是空的时候,首先是空的。然后当你首先分配到oldFirst时,这会使oldFirst为null,因此尝试访问prev会给出一个NPE。将访问权限更改为:

if (oldFirst != null)
    oldfirst.prev = first;

should fix it.

应该解决它。

Your warning is probably just because you're not atcually using prev yet in your deque, you're just setting it. Fun fact: with a deque, you don't need a doubly-linked list, so your prev variable is unlikely to ever be used. If you also want to support queuing with your deque down the road (similar to how LinkedList implements both Deque and Queue) then you'll need to keep prev.

你的警告可能只是因为你在骗局中没有使用prev,你只是设置它。有趣的事实:使用双端队列,您不需要双向链表,因此不太可能使用您的prev变量。如果你还想支持你的deque在路上排队(类似于LinkedList如何实现Deque和Queue),那么你需要保持流行。