Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 队列中出现nullpointer异常_Java_Queue - Fatal编程技术网

Java 队列中出现nullpointer异常

Java 队列中出现nullpointer异常,java,queue,Java,Queue,我相信错误是由于不正确地添加到队列中造成的,可能还有其他错误,但我认为这是一些原因 public void add(E data) { if(size == 0) { size++; front = new ListNode(data,null); } else { size++;

我相信错误是由于不正确地添加到队列中造成的,可能还有其他错误,但我认为这是一些原因

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;
        }
    }
公共作废添加(E数据)
{
如果(大小==0)
{
大小++;
front=新的ListNode(数据,空);
}
其他的
{
大小++;
ListNode温度=结束;
温度设置下一步(空);
temp=temp.getNext();
温度设置数据(数据);
结束=温度;
}
}
如果您需要剩下的代码来查找错误,这里是完整的类

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;
    }
}
import java.util.*;
公共类队列
{
私有节点前端;
私有列表节点端;
私有整数大小;
公共队列()
{
front=null;
end=null;
尺寸=0;
}
公共E peek()
{
返回front.getData();
}
公共E删除()
{
如果(size==0){返回null;}
其他的
{
ListNode温度=前;
front=temp.getNext();
大小--;
返回temp.getData();
}
}
公共空白添加(E数据)
{
如果(大小==0)
{
大小++;
front=新的ListNode(数据,空);
}
其他的
{
大小++;
ListNode温度=结束;
温度设置下一步(空);
temp=temp.getNext();
温度设置数据(数据);
结束=温度;
}
}
公共布尔值为空()
{
如果(大小==0)
返回true;
其他的
返回false;
}
//[数据,数据,数据,数据]
公共字符串toString()
{
字符串s=“”;
ListNode温度=前;
while(temp.getNext()!=null)
{
s+=temp.getData()+“,”;
}
返回s;
}
公共整数大小()
{
返回大小;
}
}
这是我正在使用的节点

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;}
} 
公共类ListNode
{
私人电子数据;
私有listnodenext;
/**
*使用指定的数据和下一步构造ListNode
*@param d节点的数据
*@param n下一个引用
*/
公共ListNode(ED,ListNode n)
{
数据=d;
n=下一个;
}
/**
*从节点返回数据
*@返回数据字段
*/
public E getData(){return data;}
/**
*设置节点的数据
*@param d新的数据字段
*/
public void setData(ed){data=d;}
/**
*获取节点的下一个引用
*@返回下一个引用
*/
public ListNode getNext(){return next;}
/**
*设置节点的下一个引用
*@param n新的下一个引用
*/
public void setNext(listnoden){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") ;

    }          
}
公共类队列测试器
{
公共静态void main(字符串参数[])
{
队列=新队列();

对于(int k=1;k您的
add
方法未正确实现,将始终只更改
前面的

当您调用时,最终调用
remove()

这将抛出一个
NullPointerException

回顾一下你的逻辑,如果你在列表中添加了一些东西,你可能应该增加它的大小

另外,请注意在
add()方法中

ListNode <E> temp = end;
temp.setNext(null);
ListNode temp=end;
温度设置下一步(空);
也将失败,因为
end
最初为
null

请尝试此操作

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++;
    }
if(大小==0)
{
front=新的ListNode(数据,空);
结束=第一个;
大小++;
}
其他的
{
ListNode temp=新ListNode(数据);
结束。设置下一步(温度);
end=end.getNext();
大小++;
}

您在使用它之前不会初始化
结束
。而且,大小永远不会增加。我不明白您想做什么say@user3025774由于添加第一个元素时不增加
size
,因此队列不会增长,只需重新分配
front
变量。队列包含一个对象,但大小为
size
0。我使用size++@user3025774增加大小,您可以在
else
块中执行,而不是在
if
中执行。基本上,
else
分支永远不会执行。
ListNode <E> temp = end;
temp.setNext(null);
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++;
    }