Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
    
public ArrayList<Walls> walls = new ArrayList<Walls>(); –  user1516873 Jun 18 '13 at 8:18

3 Answers 3

up vote 4 down vote accepted

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>();
share|improve this answer
2  
Even better, perhaps - public List<Walls> walls = new ArrayList<Walls>(); –  vikingsteve Jun 18 '13 at 8:18
    
I'm pretty freaking stupid. I even spent over 30 minutes looking and changing values around in my Wall class... Haha –  Shawn Jun 18 '13 at 8:19
    
Things like this only have to be understood once, don't worry you'll get a hold of it pretty soon I'm sure. –  Peter Jaloveczki Jun 18 '13 at 8:20

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

share|improve this answer
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.