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.
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>();
You never initialize the ArrayList; you've reserved a place in memory for it, but until you initialize it its value is null.
public ArrayList<Walls> walls = new ArrayList<Walls>();
walls
是一个引用,它必须指向一个对象(通常使用new
创建),然后再调用它上面的任何方法。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.