0
public class GroceryList {
    //variables
    private int count;
    private Item list[];
    public final int MAX = 30;

    //default
    public GroceryList(){
        Item placeholder1 = new Item();
        Item placeholder2 = new Item();
        list[0] = placeholder1;
        this.count = 0;
    }
}

I want to assign the Object placeholder1 to list[0] but I get a NullPointerException.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – null_awe Dec 05 '20 at 02:32
  • 2
    Until you assign a value to the array, it doesn't yet exist. It's like having a parking spot -- it's just a spot that fits a car, until you park a car there. You can't tell someone to sit in the driver's seat before there's a car. – Roddy of the Frozen Peas Dec 05 '20 at 02:35

1 Answers1

2

You need to initialize the array; otherwise, it is null.

public final int MAX = 30;
private Item[] list = new Item[MAX];
Unmitigated
  • 76,500
  • 11
  • 62
  • 80