Daniel Dimitrov
Daniel Dimitrov

Reputation: 5

multidimentional ArrayList[][], is it even possible?

so I have a task to make a field for a game. I can have numerous items on every field and the size of the field is chosen by the user. My question is how can I do that. I tried with an array ArrayList, but every time I try to add a value to it, I get a NullPointException. How can I solve this problem?

This is what I've come up with. 'TypeInfo' is an array String(the problem isn't in it, I checked), but I get the exception at its line:

List<String[]>[][] items = new ArrayList[x][y];
itemBoard[0][1].add(typeInfo);

Upvotes: 0

Views: 102

Answers (1)

djechlin
djechlin

Reputation: 60788

Of course, itemBoard[0][1] isn't initialized to anything. NullPointerException means something is null, so find the null thing and make sure it's not null.

List[][] items = new ArrayList[x][y];
itemBoard[0][1] = new ArrayList<>(); //java 7 shortcut
itemBoard[0][1].add(typeInfo);

Upvotes: 6

Related Questions