0

First I instantiate a gamestate

class GameState extends state{
ArrayList<Level> levels; 
int currentLevelID;
public GameState() {
    stateID = 2;
    levels = new ArrayList<Level>();
    createLevels();
    currentLevelID = 0;
}

which creates levels

    public void createLevels(){
    try {
        this.levels.add(new NormalLevel(0, 10, 10, null, null, new EmptyTile(1, 1, 1, 1, null) ));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

using this bit of code (forgot the technical term lol)

    //level is the superclass of normallevel or whatever i named the default level
    public Level(int id, int height, int width, ArrayList<TileEntity> tiles, ArrayList<MobileEntity> mobiles, TileEntity fillBlock){
    this.height = height;
    this.levelID = id;
    this.width = width;
    if(tiles != null){
        this.tiles = tiles;
    } else {
        tiles = new ArrayList<TileEntity>();
        try {
            tiles.add(new EmptyTile(-50,-50,1,1,null));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(mobiles != null){
        this.mobiles = mobiles;
    } else {
        mobiles = new ArrayList<MobileEntity>();
        try {
            mobiles.add(new DefaultEntity(-50,-50,1,1,null));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(fillBlock != null){
        this.tiles = new ArrayList<TileEntity>();
        this.fillWith(fillBlock);
    }
}
public void fillWith(TileEntity tile){
    for(int i = 0; i < this.height; i++){
        for(int j = 0; j < this.width; j++){
            for(int k = 0; k < this.tiles.size(); k++){
                if (this.tiles.get(k).y == i && this.tiles.get(k).x == j){
                    break;
                }
                if(k == this.tiles.size()){
                    tile.x = j;
                    tile.y = i;
                    this.tiles.add(tile);
                }
            }
        }
    }
}

Then I try to update the level

    public void update(){
    this.draw();
    for(int i = 0; i < this.tiles.size(); i++){
        this.tiles.get(i).update();
    }
    for(int i = 0; i < this.mobiles.size(); i++){ //nullpointerexception here
        this.mobiles.get(i).update();
    }

    this.specialUpdate();
    }

But I get an error on the commented line. I'm confused as hell, help would be appreciated :)

2
  • Looking at code it looks like mobiles arraylist is null Commented Dec 18, 2013 at 21:34
  • 2
    I suspect you actually want this.mobiles = new ArrayList<MobileEntity>(); Commented Dec 18, 2013 at 21:36

2 Answers 2

2

Your mobiles object is null because in the following code

if(mobiles != null){
        this.mobiles = mobiles;
} else {
    mobiles = new ArrayList<MobileEntity>();

in the else case your assigning the local variable mobiles rather than this.mobiles

Sign up to request clarification or add additional context in comments.

2 Comments

this is not a good place for initializing mobiles, besides this.mobiles are still null.
@klarki I did't suggest to initialise mobiles there. I just gave the reason for the NPE
1
if(mobiles != null){
    this.mobiles = mobiles;
} else {
    mobiles = new ArrayList<MobileEntity>();
    try {
        mobiles.add(new DefaultEntity(-50,-50,1,1,null));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

If mobiles != null you assign the instance variable (this.mobiles), yet if they are, you create a new List, but assign it to the local variable (mobiles), which is why the list never reaches the instance variable.

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.