-1

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
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)

I've checked and it's nothing wrong with the dishID-1 but the offer method but the problem persists

People have told me that I've instantiated the dishQueues which is an arraylist of queue integer but I never instantiated any queue integer in each addition to the array-list. I've tried it but it still gives me NPE.

How can I properly do this?

  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         ArrayList<Queue<Integer>> dishQueues = new ArrayList<Queue<Integer>>();
 22         for(int i=0;i<this.numDishes;i++)   {
 23             dishes[i] = new String(sc.nextLine());
 24             dishQueues.add(new LinkedList<Integer>());
 25         }
 26     }
 27 
 28     // Add new order to the list
 29     public void addNewOrder(int dishID, int tag) {
 30         dishQueues.get(dishID-1).offer(new Integer(tag));
 31     }   
 32     
 33     // Process food when it is ready
 34     // Return the customer who currently ordered for the dish
 35     // if there is no customer order for this dish return -1
 36     public int processReadyFood(int dishID) {
 37         int ready=0;
 38         if (dishQueues.get(dishID-1).peek() == null)
 39             return -1;
 40         else    
 41             ready = dishQueues.get(dishID-1).poll();
 42             
 43         return ready;
 44     }   
 45     
 46     // Get dish's name
 47     public String getDishName(int dishID) {
 48         return dishes[dishID-1];
 49     }   
 50     
 51 }
 52 
 53 public class QuickEat {
 54 
 55     public static void main(String [] args) {
 56     
 57         Scanner sc = new Scanner(System.in);
 58         int numDishes = sc.nextInt();
 59         String command; 
 60         sc.nextLine();
 61 
 62         // Create the list order of food
 63         ListOrder listOrder = new ListOrder(numDishes, sc);
 64 
 65         int noOfCommands = sc.nextInt();
 66         sc.nextLine();
 67 
 68         // Process commands
 69         for(int i=0; i<noOfCommands;i++)    {
 70             command = sc.next();
 71             if(command.equals("Order")) {
 72                 int tag = sc.nextInt();
 73                 int numOfDishes = sc.nextInt();
 74                 for(i=0;i<numOfDishes;i++)
 75                     listOrder.addNewOrder(sc.nextInt(),tag);
 76             }
 77             else if(command.equals("Ready"))    {
 78                 int dishID = sc.nextInt();
 79                 int customer = listOrder.processReadyFood(dishID);
 80                 if(customer == -1)
 81                     System.out.println("Throw away " + listOrder.getDishName(dishID));
 82                 else
 83                     System.out.println(listOrder.getDishName(dishID) + " ready to be served to Tag " + customer);
 84             }
 85         }
 86     }
 87 }
3
  • 1
    possible duplicate of NullPointerException in queue/linkedlist program Commented Mar 17, 2014 at 16:00
  • 1
    You just asked this question an hour ago, got an answer, and accepted it. Flagged as duplicate. Commented Mar 17, 2014 at 16:01
  • @EricStein you're a great spoiler (: just trying my best to learn Commented Mar 17, 2014 at 16:15

1 Answer 1

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

Replace the line above in your constructor with this one:

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

You already declared the dishQueues ArrayList as an instance variable. You must initialize it in the constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. somehow that worked. I guess when I did that in the constructor i was creating another instance which was not the attribute of listorder which was why i kept on getting NPE
You originally made your dishQueue ArrayList as an arraylist that only exists inside the constructor. (You didn't initialize the arraylist instance variable to be used globally). As a result, whenever you called addNewOrder, calling .get() on something that is null will throw the NPE.

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.