1

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.

1
  • public ArrayList<Walls> walls = new ArrayList<Walls>(); Commented Jun 18, 2013 at 8:18

3 Answers 3

4

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>();
Sign up to request clarification or add additional context in comments.

3 Comments

Even better, perhaps - public List<Walls> walls = new ArrayList<Walls>();
I'm pretty freaking stupid. I even spent over 30 minutes looking and changing values around in my Wall class... Haha
Things like this only have to be understood once, don't worry you'll get a hold of it pretty soon I'm sure.
0

You never initialize the ArrayList; you've reserved a place in memory for it, but until you initialize it its value is null.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.