0

I'm still new to programming and I want to make a program that will take the food order from user until the user presses "n" to stop. But I can't seem to make it work like I want it to.

I want my output to be like this.

Buy food: Burger
Order again(Y/N)? y
Buy Food: Pizza
Order again(Y/N)? n

You ordered: 
Burger
Pizza

But my output right now is this.

Buy food: Burger
Order again(Y/N)? y
Buy food: Pizza
Order again(Y/N)? n

You ordered: 
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Array.getFoodName()" because "food_arr2[i]" is null
    at Food.main(Food.java:50)

Here is my code:

public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Food food = new Food();  
        Array[] food_arr;

        boolean stop = false;
        String foodName;
        int k = 1;
        int j = 0;
        
        while(stop == false) {
            food_arr = new Array[k];

            System.out.print("Buy food: ");
            foodName = s.next();
            food_arr[j] = new Array(foodName);
            food.setFoodArray(food_arr);

            System.out.print("Order again(Y/N)? ");
            String decide = s.next();
            if(decide.equalsIgnoreCase("y")) {
                k++;
                j++;
            }
            else if(decide.equalsIgnoreCase("n")) {
                stop = true;
            }
        }

        Array[] food_arr2 = food.getFoodArray();

        for (int i = 0; i < food_arr2.length; ++i) {
            System.out.println("\nYou ordered: ");
            System.out.println(food_arr2[i].getFoodName()); //This line is the error according to my output
        }
    }

I don't know how to fix this and I was hoping for someone to help me.

hedgeCoder
  • 11
  • 1
  • the error is coming because `food.getFoodArray()` is returning null.Why don't you try checking if it is null by `if(food.getFoodArray() == null)`? – Sambhav Khandelwal Jan 30 '22 at 08:57
  • @SambhavKhandelwal The exception does not indicate the array itself is null, it indicates the value at index i of the array (`foo_arr2[i]`) is null – Mark Rotteveel Jan 30 '22 at 09:42

1 Answers1

0

I think I see what you are trying to do with the k value setting the size of the array you are using. However, with each iteration of the while loop:

food_arr = new Array[k];

Will create a new empty array each time!

So, for example, on the second iteration

food.setFoodArray(food_arr);

Will set foods array as something like [null, "Pizza"]

Even if this did work, creating a new array each time is not a very efficient method.


I would strongly recommend using a different, dynamically allocated data structure such as an ArrayList and defining it outside the scope of the while loop.

ArrayList<Food> food_arr = new ArrayList<Food>() 
// Note that I'm just guessing the data type here - I can't see what you are actually using!   

while(stop == false) {

        System.out.print("Buy food: ");
        foodName = s.next();
        food_arr.add(foodName)
        
        // etc, etc
    }

food.setFoodArray(food_arr) 
// ! Note: You will need to convert the array list into an array
// ! or change the data struture in the Food class

// etc, etc

However, this is just the first solution that popped into my head, check out different kinds of data structures and think about how else you could design this program yourself!