piggyback
piggyback

Reputation: 9254

Java - Populating an ArrayList gives null pointer error

In my class I define:

private ArrayList<BlockObject> blocks;

And later:

blocks.add(new BlockObject(x, y));

However I get this error

02-22 17:06:52.672: E/AndroidRuntime(479): Caused by: java.lang.NullPointerException
02-22 17:06:52.672: E/AndroidRuntime(479):  at com.comp1008.hhh.uuu.Scenario.blocks(Scenario.java:41)

Any suggestion?

Upvotes: 1

Views: 142

Answers (2)

Adrian Jandl
Adrian Jandl

Reputation: 3015

private ArrayList<BlockObject> blocks = new ArrayList<BlockObject>();

You have to initialize your object.

Upvotes: 7

PermGenError
PermGenError

Reputation: 46408

you have to initialize your arraylist before you populate it.

private ArrayList<BlockObject> blocks = new ArrayList<BlockObject>(); 

Upvotes: 5

Related Questions