0

I'm currently working on some code, and still being a beginner in java, i'm having a little trouble figuring out this problem, and would like to get some advice on what I did wrong, and can do to fix it.

The code that will be below throws me a null pointer exception on the line where the entry in the array is added in the loop

public class World {


int mapWidth;
int mapHeight;

Tile testTile;
Tile[][] map;


public World(int width, int height)
{
    testTile = new Tile(0,0, 100);
    mapWidth = width;
    mapHeight = height;
}

public World()
{
    mapWidth = 10;
    mapHeight = 10;

    testTile = new Tile(0,0, 100);
}

public void generate()
{
    for(int y = 0; y <= mapHeight; y++)
    {
        System.out.println("Y: " + y);
        for (int x = 0; x <= mapWidth; x++)
        {
            System.out.println("X: " + x);
            map[x][y] = new Tile(x, y, 100);
        }
    }
}

public void render(SpriteBatch batch)
{

}

}

Any advice is greatly appreciated, thanks for any help that you can give!

  • 2
    You need to assign a value to `map`, e.g. `map = new Tile[mapWidth][mapHeight];` – Andy Turner Jun 01 '15 at 21:09
  • You need create array of Tile[][] before add new entries. In java every object (array is also object), should be created with 'new' before it could be used. Before this moment there is no object in memory, there is only link to 'null', that is why you get NullPointerException. – sphinks Jun 01 '15 at 21:12

0 Answers0