Reputation: 15
I'm a bit new to java so please dumb down the answers as if I'm a four year old. =-D
I'm using slick for this code. If you need more code please just ask. I'm trying to post only what I see as relevant, but as I'm new to programming I may be wrong.
I've tracked the origin of the problem to adding a newly created object into an arrayList. Here's the code:
public ArrayList<Walls> walls;
Walls w1 = new Walls(new RPoint(100,100), brickwall, brickwall, 100);
walls.add(w1); //this is where the error will occur at.
Walls class:
package main;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
public class Walls {
private RPoint p;
private Image i;
private Image i2;
private int durability;
//private Rectangle r = new Rectangle (1,1,1,1);
public Walls(RPoint a, Image b, Image c, int d){
this.p=a;
this.i=b;
this.i2=c;
this.durability=d;
// Rectangle r = new Rectangle(Math.round(a.getX()), Math.round(a.getY()), Math.round(b.getWidth()), Math.round(b.getHeight()));
}
public Image getImage()
{
return this.i;
}
// public Rectangle getRect()
// {
// return this.r;
// }
public Image getSecondaryImage()
{
return this.i2;
}
public int getLife()
{
return this.durability;
}
public RPoint getPoint()
{
return this.p;
}
}
Thanks for any help or for even looking.
Upvotes: 1
Views: 1347
Reputation: 13872
public ArrayList<Walls> walls = new ArrayList<Walls>();
walls
is a reference that must point to an object (usually created using new
) before invoking any methods on it.
Upvotes: 0
Reputation: 247
You never initialize the ArrayList; you've reserved a place in memory for it, but until you initialize it its value is null.
Upvotes: 0
Reputation: 2089
The arrayList must be initialized! : )
public ArrayList<Walls> walls = new ArrayList<Walls>();
Edit:
True that the conventional way to declare such a field is to use the interface as type like so:
public List<Walls> walls = new ArrayList<Walls>();
Upvotes: 4