Java 如何修复通用循环数组队列中的NullPointerException

Java 如何修复通用循环数组队列中的NullPointerException,java,arrays,for-loop,nullpointerexception,queue,Java,Arrays,For Loop,Nullpointerexception,Queue,我正在尝试使用两个队列创建一个基本的机场模拟。它们应该在起飞之间交替,当一个没有更多的“飞机”时,需要处理NPE,以便另一个队列可以继续运行,直到循环结束,两个队列都已用完,因为一个有7个条目,另一个有4个条目。我想,如果我试图在dequeue方法中放置一个异常处理程序,它可能会修复它,但我必须为它使用一个泛型类,因此这无法正常工作。从现在开始,打算出列的for循环一直运行到第四个循环,也就是它停止、抛出NPE并退出程序的时候。所以我的问题是,我如何使一个队列用完时,它停止尝试循环它,而是继续循

我正在尝试使用两个队列创建一个基本的机场模拟。它们应该在起飞之间交替,当一个没有更多的“飞机”时,需要处理NPE,以便另一个队列可以继续运行,直到循环结束,两个队列都已用完,因为一个有7个条目,另一个有4个条目。我想,如果我试图在dequeue方法中放置一个异常处理程序,它可能会修复它,但我必须为它使用一个泛型类,因此这无法正常工作。从现在开始,打算出列的for循环一直运行到第四个循环,也就是它停止、抛出NPE并退出程序的时候。所以我的问题是,我如何使一个队列用完时,它停止尝试循环它,而是继续循环最后一个队列

这是我的主类,循环正在运行:

public abstract class Airport<T> {

    public static void main(String[] args) {
        //creates separate queues
        Driver<Integer> runway1 = new Driver<>();
        Driver<Integer> runway2 = new Driver<>();

        //adds 7 planes into runway 1
        for (int i = 0; i < 7; i++) {
            runway1.enqueue(i);
        }

        //adds 4 planes into runway 2
        for (int i = 0; i < 4; i++) {
            runway2.enqueue(i);
        }

        int size1 = runway1.length();
        int size2 = runway2.length();

        //run loop while either runway has planes left
        while (size1 > 0 && size2 > 0) {
            System.out.println("There are currently " + size1 + " planes waiting for takeoff in Runway 1.");
            System.out.println("There are currently " + size2 + " planes waiting for takeoff in Runway 2.\n");


            for (int lane = 0; lane < 7; lane++) {
                int lane1, lane2;

                if (runway1.isEmpty()){
                    System.out.println("Runway 1 is empty.");
                }
                else {
                    lane1 = runway1.getFront();
                    System.out.println("Plane " + lane1 + " has taken off from Runway 1.");
                    runway1.dequeue();
                }

                if (runway2.isEmpty()){
                    System.out.println("Runway 2 is empty.");
                }
                else{
                    lane2 = runway2.getFront(); //EDIT: npe occurs here
                    System.out.println("Plane " + lane2 + " has taken off from Runway 2.\n");
                    runway2.dequeue();
                }

            }
        }

    }

}
公共抽象类机场{
公共静态void main(字符串[]args){
//创建单独的队列
驾驶员跑道1=新驾驶员();
驾驶员跑道2=新驾驶员();
//在跑道1上增加7架飞机
对于(int i=0;i<7;i++){
跑道1.排队(i);
}
//在跑道2中增加4架飞机
对于(int i=0;i<4;i++){
跑道2.排队(i);
}
int size1=跑道1.length();
int size2=跑道2.length();
//在任何一条跑道上还有飞机的情况下运行环路
而(大小1>0和大小2>0){
System.out.println(“目前有“+size1+”架飞机在1号跑道等待起飞。”);
System.out.println(“目前有“+size2+”架飞机在跑道2等待起飞。\n”);
对于(int lane=0;lane<7;lane++){
int lane1、lane2;
if(runway1.isEmpty()){
System.out.println(“跑道1为空”);
}
否则{
lane1=runway1.getFront();
系统输出打印(“飞机”+lane1+“已从1号跑道起飞”);
跑道1.dequeue();
}
if(runway2.isEmpty()){
System.out.println(“跑道2为空”);
}
否则{
lane2=runway2.getFront();//编辑:此处出现npe
System.out.println(“飞机”+lane2+“已从2号跑道起飞。\n”);
跑道2.dequeue();
}
}
}
}
}
这是我的驾驶员课程的一部分:

class Driver<T> implements Queue1<T> {
    private static final int defaultSize = 10;
    private int maxSize; // Maximum size of queue
    private int front; // Index of front element
    private int rear; // Index of rear element
    private T[] listArray; // Array holding queue elements

    /**
     * Constructors
     */
    Driver() {
        this(defaultSize);
    }

    @SuppressWarnings("unchecked")
        // For generic array
    Driver(int size) {
        maxSize = size + 1; // One extra space is allocated
        rear = 0;
        front = 1;
        listArray = (T[]) new Object[maxSize]; // Create listArray
    }

    /**
     * Put "it" in queue
     */
    public void enqueue(T it) {
        assert ((rear + 2) % maxSize) != front : "Queue is full";
        rear = (rear + 1) % maxSize; // Circular increment
        listArray[rear] = it;
    }

    /**
     * Remove and return front value
     **/
    public T dequeue() {
        assert length() != 0 : "Queue is empty";
        T it = listArray[front];
        front = (front + 1) % maxSize; // Circular increment
        return it;
    }

    /**
     * Return Front Value
     **/
    @Override
    public T getFront() {
        assert length() != 0 : "Queue is empty";
        return listArray[front];
    }

    @Override
    public T getBack() {
        assert length() != 0 : "Queue is empty";
        return listArray[rear];
    }

    @Override
    public boolean isEmpty() {
        if(listArray == null){
            return true;
        }
        return false;
    }

    /**
     * @return Queue length
     **/
    public int length() {
        return ((rear + maxSize) - front + 1) % maxSize;
    }


}


类驱动程序实现队列1{
私有静态最终int defaultSize=10;
private int maxSize;//队列的最大大小
private int front;//front元素的索引
private int-rear;//后元素的索引
private T[]listArray;//包含队列元素的数组
/**
*建设者
*/
司机(){
这(默认大小);
}
@抑制警告(“未选中”)
//对于泛型数组
驱动程序(整数大小){
maxSize=size+1;//分配了一个额外的空间
后部=0;
正面=1;
listArray=(T[])新对象[maxSize];//创建listArray
}
/**
*把“它”放在队列中
*/
公共无效排队(T it){
断言((后+2)%maxSize)!=前:“队列已满”;
后=(后+1)%maxSize;//循环增量
listArray[rear]=它;
}
/**
*删除并返回前值
**/
公共T出列(){
断言长度()!=0:“队列为空”;
T it=listary[front];
前=(前+1)%maxSize;//循环增量
归还它;
}
/**
*返回前值
**/
@凌驾
公共T getFront(){
断言长度()!=0:“队列为空”;
返回listArray[前端];
}
@凌驾
公共T getBack(){
断言长度()!=0:“队列为空”;
返回列表数组[后];
}
@凌驾
公共布尔值为空(){
if(listArray==null){
返回true;
}
返回false;
}
/**
*@返回队列长度
**/
公共整数长度(){
返回((后部+最大尺寸)-前部+1)%maxSize;
}
}

这几天我一直在挠头,所以任何帮助都将不胜感激!谢谢大家!

问题是您的
isEmpty()
方法没有按您希望的方式工作。您正在使用它,就好像它检查队列中是否有任何内容,但它是为了执行其他操作而编写的。假设
length()
方法正确,则需要检查
length()==0
。如果是,那么您应该返回true。如果不是,则应返回false

获取NullPointerException的原因是,您假设列表不是空的,所以您尝试访问队列中的下一个对象,即
null

在代码中,它将如下所示:

public boolean isEmpty() {
   return length()==0;
}

我没有你的驾驶课,所以我只好将就一下:

public static void main(String [] args) {
    ArrayDeque<Integer> runway1 = new ArrayDeque<>();
    ArrayDeque<Integer> runway2 = new ArrayDeque<>();
    //adds 7 planes into runway 1
    for (int i = 0; i < 7; i++) {
        runway1.offer(i);
    }

    //adds 4 planes into runway 2
    for (int i = 0; i < 4; i++) {
        runway2.offer(i);
    }

    //run loop while either runway has planes left
    String waitingMsg = "There are currently %d planes waiting for takeoff in Runway %d.";
    while (!runway1.isEmpty() && !runway2.isEmpty()) {
        System.out.println(String.format(waitingMsg, runway1.size(), 1));
        System.out.println(String.format(waitingMsg, runway2.size(), 2));
        System.out.println();

        for (int lane = 0; lane < 7; lane++) {
            if (runway1.isEmpty()){
                System.out.println("Runway 1 is empty.");
            } else {
                int lane1 = runway1.pop();
                System.out.println("Plane " + lane1 + " has taken off from Runway 1.");
            }

            if (runway2.isEmpty())  {
                System.out.println("Runway 2 is empty.");
            } else{
                int lane2 = runway2.pop();
                System.out.println("Plane " + lane2 + " has taken off from Runway 2.\n");
            }

        }
    }

}
publicstaticvoidmain(字符串[]args){
ArrayDeque跑道1=新的ArrayDeque();
ArrayDeque跑道2=新的ArrayDeque();
//在跑道1上增加7架飞机
对于(int i=0;i<7;i++){
跑道1.报价(一);
}
//在跑道2中增加4架飞机
对于(int i=0;i<4;i++){
跑道2.报价(一);
}
//在任何一条跑道上还有飞机的情况下运行环路
String waitingMsg=“目前有%d架飞机在跑道%d等待起飞”;
而(!runway1.isEmpty()&&!runway2.isEmpty()){
System.out.println(String.format(waitingMsg,runway1.size(),1));
System.out.println(String.format(waitingMsg,runway2.size(),2));
System.out.println();
对于(int lane=0;lane<7;lane++){
if(runway1.isEmpty()){
System.out.println(“跑道1为空”);
}否则{
我