0

I am simulating an airport which has only one runway. When it is busy, planes wishing to take off or land have to wait. Implement a simulation, using two queues, one each for the planes waiting to take off and land. Landing has the higher priority. The user enters commands takeoffflightSymbol, landflightSymbol, next, and quit. The first two commands place the flight in the appropriate queue. The next command finishes the current takeoff or landing and enables the next one, printing the action (takeoff or land) and the flightsymbol. But I can't seem to find the problem for the following code. Help!

public class Runway
{
   public Scanner in;
   public Queue<String> takingOff;
   public Queue<String> landing;

   public Runway()
   {
       in = new Scanner(System.in);
       boolean done = false;
       while (!done)
       {
          System.out.println("Type TAKEOFF/LAND followed by Fight Number to Queue a plane");
          System.out.println("Type NEXT to perform the next action, or QUIT to close the simulation");
          String action = in.next();
          if (action.equals("TAKEOFF"))
          {
              String flight = in.next();
              takingOff.add(flight);
          }
          else if (action.equals("LAND"))
          {
              String flight = in.next();
              landing.add(flight);
          }
          else if (action.equals("NEXT"))
          {
              handleNextAction();
          }
          else if (action.equals("QUIT"))
          {
              System.exit(0);
          }
          else {
              System.out.println("ERROR - not a valid command");
          }
       }
   }

   public void handleNextAction()
   {
       if (landing.size() > 0)
       {
          String flight = landing.remove();
          System.out.println("Flight " + flight + " is landing. ");
       }
       else if (takingOff.size() > 0)
       {
          String flight = takingOff.remove();
          System.out.println("Flight " + flight + " is taking off.");
       }
       else
       {
          System.out.println("There are no flights waiting to take off or land");
       }
   }

   public static void main (String[] args)
   {
       Runway simulator = new Runway();
   }
}

When the simulation is running, other than the quit function, all the other functions produce java.lang.NullPointerException errors.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user2972181
  • 15
  • 1
  • 5
  • 2
    You never initialize `takingOff` and `landing` , both are `null`. – Arnaud Feb 21 '17 at 14:51
  • 1
    The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Feb 21 '17 at 15:03

0 Answers0