Map Generation - Java2D

Hello everyone :slight_smile:

I recently made a rpg game using the java standard library. To generate the map i have used a class to read from a image and made the array of tiles.

Example: An image of 20x20 is going to be an array of [20][20].

All the game is working perfectly. But I’ve just made maps like 20x15 ,40x40 etc.

Recently I tried to make 1000x1000 to see what happens ,when i run the game take 1 or 2 seconds to load all the map but keep working without dropping the performance. Just some lagging at begining but after 30 seconds the speed keeps the same as before.

Then i tried to make 5000x5000 and at the moment i start moving a have an error of
“NULL POINTER EXCEPTION”.

But I cant figure out why this happens, because my first class that i create is the World class and create all the tiles and after that is created the player class.

My question is: Why this happens if I create the player class after the world has been loaded.

Thank you…

I don’t know completely how you make up your world, but this error can come from and Array for example if you have an array at at some point you have defined it has [10][20] and then you try to access the ‘tile’ @ [10][21].render() for example you’ll get a null. So I would check to see if you remembered to update the dimensions when you changed the map size.

Good luck.

BC

@BeardedCow The situation you described would throw an ArrayOutOfBoundsException, not a NullPointerException.

@OP Are you making sure that you are looping through every tile and initializing it like [icode]map[x][y] = new Tile();[/icode]

Sorry what is was trying to say that if you haven’t gone through them all once to initialize then you’ll return null. My fault for not making it clearer, anyway you put it a lot simpler.

Yes of course,before i create the object i define the size based on the image.

Example:

int w = image.getWidth();
int h = image.getHeight();
block = new Block[w][h]

Im gona post the code this afternoon,because im not at home right now

…but you have to initialize every block[0][1], block[0][2]… they’re all null.

I don’t think that is the issue since guigui said that a map the size of 1k by 1k works, unless something was fundamentally changed which case, even a map the size of 20 by 20 wouldn’t work.

Could it be an issue of memory capacity? Or would there be an exception for that?

Fixed,probably its a memory issue because its to much blocks.

The solution was a simple line:


if(block[x][y] != null){
  //Do the stuff
}

In that case you should handle the memory exception at source rather than add the null check. Am I right it makes no sense to have a null tile?

It sounds like you are catching the exception but just logging it or something like that, so the app keeps running even though it can’t build the map. The propblem is, if you can’t build the map, it is probably a fatal error even though you can technically eat the exception.

sorry,my mistake at the map creation.

The image was 5000x5000,instead of use pencil to make pixel per pixel i selected the pen tool at the editor so automatically add other colors instead of my default block color (white).
My code only handle this type of color.

No need to use:

if(block[xx][yy] != null)