队列引起的nullpointer异常

时间:2013-11-25 22:13:19

标签: java queue

我认为错误是由错误添加到队列引起的,可能还有其他错误,但我认为这是在这里

public void add(E data)
    {
        if(size == 0)
        {
                      size++;
            front = new ListNode(data,null);
        }
        else
        {
                    size++;
            ListNode <E> temp = end;
            temp.setNext(null);
            temp = temp.getNext();
            temp.setData(data);
            end = temp;
        }
    }

如果您需要其余代码来查找错误,那么这就是完整的类

import java.util.*;

public class Queue<E>
{
        private ListNode <E> front;
        private ListNode <E> end;
        private int size;

    public Queue()
    {
        front = null;
        end = null;
        size = 0;
    }

    public E peek()
    {
        return front.getData();
    }

    public E remove()
    {
        if(size == 0){return null;}
        else
        {
        ListNode <E> temp = front;
        front = temp.getNext();
        size--;
        return temp.getData();
        }

    }

    public void add(E data)
    {
        if(size == 0)
        {
                     size++;
            front = new ListNode(data,null);
        }
        else
        {
                    size++;
            ListNode <E> temp = end;
            temp.setNext(null);
            temp = temp.getNext();
            temp.setData(data);
            end = temp;
        }
    }

    public boolean isEmpty()
    {
        if (size == 0)
            return true;
        else
            return false;

    }

    // [data, data, data, data]
    public String toString()
    {
        String s ="";
        ListNode <E> temp = front;
        while(temp.getNext()!= null)
        {
            s+=temp.getData() + ", ";
        }

        return s;
    }

    public int size()
    {
        return size;
    }
}

这是我正在使用的节点

public class ListNode<E> 
{
    private E data;
    private ListNode<E> next;

    /**
     * Constructs a ListNode with a specified data and next
     * @param d the data for the node
     * @param n the next reference
     */
    public ListNode(E d, ListNode<E> n) 
    {
        data = d;
        n = next;
    }

    /**
     * returns the data from the node
     * @return the data field
     */
    public E getData() {return data;}

    /**
     * sets the data for the node
     * @param d the new data field
     */
    public void setData(E d) {data = d;}

    /**
     * gets the next reference of the node
     * @return the next reference
     */
    public ListNode<E> getNext() { return next; }

    /**
     * sets the next reference for the node
     * @param n the new next reference
     */
    public void setNext(ListNode<E> n) { next = n;}
} 

这就是我用它来测试它

public class QueueTester
{
    public static void main (String args[])
    {
        Queue<Integer> queue = new Queue<Integer>();

        for (int k = 1; k <= 100; k++) // add 1 to 100
            queue.add(k);

        System.out.println ("Size: " + queue.size());
        System.out.println(queue + "\n");

        for (int k = 1; k <= 50; k++) // remove 1 to 50, contents 51 to 100
        {
            int number = queue.remove();
        }

        System.out.println ("Size: " + queue.size());
        System.out.println(queue + "\n");

        for (int k = 200; k <= 500; k+=10) // add tens 200 to 500 (after 51 to 100)
            queue.add(k);

        System.out.println ("Size: " + queue.size());   
        System.out.println(queue + "\n");

        while (!queue.isEmpty()) // prints contents (should be 51 to 100, then 200 to 500 tens)
        {
            System.out.print(queue.remove() + "  ");
        } 
        System.out.println("\n");

        System.out.println ("Size: " + queue.size());
        System.out.println(queue); // empty
        System.out.println ("Remove from empty queue: " + queue.remove() + "\n") ;

    }          
}

2 个答案:

答案 0 :(得分:0)

您的add方法未正确实施,并且始终只更改front

当您致电最终致电remove()

public E remove()
{
    if(size == 0){return null;}
    else
    {
    ListNode <E> temp = front;
    front = temp.getNext();
    size--;
    return temp.getData();
    }

}

size == 0起,您将返回null。然后,您尝试将null取消引用到int

int number = queue.remove();

这将抛出NullPointerException

检查你的逻辑,如果你在列表中添加一些东西,你应该增加大小。

另请注意,在add()方法

ListNode <E> temp = end;
temp.setNext(null);
由于end最初为null

也会失败。

答案 1 :(得分:0)

试试这个..

if(size == 0)
    {
        front = new ListNode(data,null);
        end = first; 
        size++;
    }
    else
    {
        ListNode <E> temp = new ListNode(data);
        end.setNext(temp);
        end = end.getNext();
        size++;
    }