1

I am trying to create a basic airport simulation using two queues. They are supposed to alternate between taking off, and when one has no more 'planes' left, the NPE needs to be handled so that the other queue can continue going until the loop has finished and both queues have run out, as one has 7 entries and the other has 4. I thought if I tried to put an exception handler in the dequeue method it might fix it, but I have to use a generic class for it, so that didn't work properly. As of right now the for loop that is meant to dequeue runs until the 4th loop, which is when it stops, throws the NPE, and exits the program. So my question is, how can I make it so that when one queue runs out, it stops trying to loop it and instead continues looping the last queue?

Here is my main class where the loop is running:

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();
                }

            }
        }

    }

}

And here is the part of my driver class:

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;
    }


}


I've been scratching my head over this for days, so any help would be incredibly appreciated! Thank you!

Kaijudile
  • 59
  • 5

2 Answers2

2

The issue is that your isEmpty() method doesn't work the way you want it to. You are using it as if it checks whether there is anything in the queue, but it is written to do something else. Assuming your length() method is correct, you need to check whether length() == 0. If it does, then you should return true. If not, you should return false.

The reason you are getting a NullPointerException is because you are assuming your list isn't empty when it is, so you try to access the next thing in your queue, which is null

In code, it would look like this:

public boolean isEmpty() {
   return length()==0;
}
SecondThread
  • 108
  • 2
  • 11
  • oh, that was the problem! Once I change the while loop conditions to (!runway1.isEmpty() && !runway2.isEmpty()) it started working perfectly! Thank you so much! – Kaijudile Mar 12 '20 at 21:36
0

I don't have your driver class, so I made do with what's available:

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");
            }

        }
    }

}
Ryan
  • 1,762
  • 6
  • 11