[Solved] Tiled Map Layer collision null pointer exception

For the the moment I just want to print out the property I put in the tiles of a single layer if the player walks on it. I am getting a nullpointer exception every time I walk on one of those tiles for some reason and I can’t figure out why.

if (Gdx.input.isKeyJustPressed(Keys.A)) {
		
				if(collisionLayer.getCell(playerScaledX - 1, playerScaledY).getTile() != null){
			System.out.println(collisionLayer.getCell(playerScaledX  - 1, playerScaledY).getTile().getProperties().get("boolean").toString());
			
			}
				playerX -= 16;
				isUpdated = true;
			}
			
			}

This is how I’m creating my collision layer object:


 collisionLayer = (TiledMapTileLayer) MapManager.map.getLayers().get("Collision");

Are you storing the tile data properly in the collision layer? After initialization or whenever you make the tile data, print out the information all the tiles posses to see if you are.

Ok so doing this:


 System.out.println(collisionLayer.getObjects().getCount());

is printing out 0. Which means I am either creating the layer wrongs, or its looking for objects instead of tiles.

How are you creating the collision layer? What does “getObjects()” do? Showing some of this code helps me help you haha

Ok so here’s an update, using this code works if there is one of those collision tiles next to me, but as soon as I press the move button in that direction and there is no tile, the game crashes and gives me a null pointer exception error. So the collision layer is indeed working, I am just doing something illogical.

if (Gdx.input.isKeyJustPressed(Keys.A)) {
		
				if(collisionLayer.getCell(playerScaledX - 1, playerScaledY).getTile() != null){
				isUpdated = true;
			}
				else{
					playerX -= 16;
					isUpdated = true;
				}
			}

Okay, by the way, is your game a free-moving tile based game where you can be anywhere on a tile like my game Shard Master or does movement always put you in the center of the tile?

Also, do you know the coordinates/bounds of each tile in the collision layer? If so, you can just check to see if the player’s X,Y is within the rectangle that the tile represents.

Where is this nullpointer being thrown? Which line of this snippet throws it?

The game is not free moving. All my tiles are 16x16 so I have two variables called playerScaledX and playerScaledY, that take the player’s position and divide that by 16 to give me the tile number its on.

I’m getting this error

com.lok.entites.Player.update(Player.java:45)

which leads to this line of code :

if(collisionLayer.getCell(playerScaledX - 1, playerScaledY).getTile() != null){
				isUpdated = true;
			}

I don’t know how to use the debugger so I can’t for the moment provide you with more information.

“getCell()” will throw a nullpointer exception if “getCell()” returns null and you ask it to “getTile()”

Basically do this:


Cell c = collisionLayer.getCell(playerScaledX - 1, playerScaledY);
if (c != null) {
if(c.getTile() != null){
		isUpdated = true;
	}
}

Ah, thank you very much! If it wasn’t for this I wouldn’t have realized that the cells and the tiles were 2 separate objects!

Haha yeah no problem :slight_smile: