0
  1 // This program helps staff manage customers'
  2 // orders and decide who should be given a ready dish.
  3 
  4 import java.util.*;
  5 
  6 // This class represents all orders of customers
  7 class ListOrder {
  8 
  9     // Data member
 10     private int numDishes;
 11     // All dishes which the restaurant offers
 12     private String[] dishes;
 13     // Each dish has a queue of customers who ordered this dish
 14     // All such queues are put inside an ArrayList called dishQueues
 15     private ArrayList<Queue<Integer>> dishQueues;
 16 
 17     // Constructor
 18     public ListOrder(int numDishes, Scanner sc) {
 19         this.numDishes = numDishes;
 20         dishes = new String[this.numDishes];
 21         for(int i=0;i<this.numDishes;i++)   {
 22             dishes[i] = new String(sc.nextLine());
 23             dishQueues.add(new LinkedList<Integer>());
 24         }
 25     }
 26 
 27     // Add new order to the list
 28     public void addNewOrder(int dishID, int tag) {
 29         dishQueues.get(dishID-1).offer(new Integer(tag));
 30     }
 31 
 32     // Process food when it is ready
 33     // Return the customer who currently ordered for the dish
 34     // if there is no customer order for this dish return -1
 35     public int processReadyFood(int dishID) {
 36         int ready=0;
 37         if (dishQueues.get(dishID-1).peek() == null)
 38             return -1;
 39         else
 40             ready = dishQueues.get(dishID-1).poll();
 41 
 42         return ready;
 43     }
 44 
 45     // Get dish's name
 46     public String getDishName(int dishID) {
 47         return dishes[dishID-1];
 48     }
 49 
 50 }
 51 
 52 public class QuickEat {
 53 
 54     public static void main(String [] args) {
 55 
 56         Scanner sc = new Scanner(System.in);
 57         int numDishes = sc.nextInt();
 58         String command;
 59         sc.nextLine();
 60 
 61         // Create the list order of food
 62         ListOrder listOrder = new ListOrder(numDishes, sc);
 63 
 64         int noOfCommands = sc.nextInt();
 65         sc.nextLine();
 66 
 67         // Process commands
 68         for(int i=0; i<noOfCommands;i++)    {
 69             command = sc.next();
 70             if(command.equals("Order")) {
 71                 int tag = sc.nextInt();
 72                 int numOfDishes = sc.nextInt();
 73                 for(i=0;i<numOfDishes;i++)
 74                     listOrder.addNewOrder(sc.nextInt(),tag);
 75             }
 76             else if(command.equals("Ready"))    {
 77                 int dishID = sc.nextInt();
 78                 int customer = listOrder.processReadyFood(dishID);
 79                 if(customer == -1)
 80                     System.out.println("Throw away " + listOrder.getDishName(dishID));
 81                 else
 82                     System.out.println(listOrder.getDishName(dishID) + " ready to be served to Tag " + customer);
 83             }
 84         }
 85     }
 86 }

I don't have much problem with other parts of the code but if there is please do highlight to me! The main problem I get is that when I enter in

3
Fish n Chips

I keep getting this error

Exception in thread "main" java.lang.NullPointerException
        at ListOrder.<init>(QuickEat.java:23)
        at QuickEat.main(QuickEat.java:62)

and I can't seem to spot the error anywhere. any help? :/

EDITED: Thanks for the comment guys. I'm also getting another error. I think it's the same problem as you guys said with the initialisation

When I enter in

3
Fish n Chips
Chicken Chop
Grilled Salmon
9
Order 1 2 1 3

I get this error.

Exception in thread "main" java.lang.NullPointerException
        at ListOrder.addNewOrder(QuickEat.java:30)
        at QuickEat.main(QuickEat.java:75)

HELP! :(

0

2 Answers 2

2
private ArrayList<Queue<Integer>> dishQueues;

not instantiated do

private ArrayList<Queue<Integer>> dishQueues = new ArrayList<Queue<Integer>>();

before you use it.

or even

private List<Queue<Integer>> dishQueues = new ArrayList<Queue<Integer>>();
Sign up to request clarification or add additional context in comments.

7 Comments

That other issue may be related to the -1 you are using in the get.
Oh. Because the orders at set at 1 2 3 whereas in the ArrayList the first element starts at 0. So 1 2 3 corresponds to dishQueues.get(0), dishQueues.get(1) and dishQueues.get(2) respectively. I actually tried changing the dishID-1 to just a flat 0 but it still gave me an error meaning it's something to do with the offer method but I can't see why :(
maybe but then thinking about it that would give a indexoutofboundsexception (I think)
But in a linkedlist there shouldnt be an indexoutofboundsexception because to begin with there is no limit to a linkedlist and queue is an implementation of LL. so if i offer it will just add to the end of the list without considering the size because size is not needed. is what i'm saying correct or are there some flaws :/
My thought process was that you would end up with -1 (if 0 were entered) which would be out of bounds. Yes just add it and your list will expand as required.
|
1

You never initialized your dish queues array list. Initialize it in your constructor.

dishQueues = new ArrayList<Queue<Integer>>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.