[LibGdx, TiledMap] getCell returns null :S

So I’m using both LibGdx and TiledMap to create a game. Im currently working on the collision between the player and the map, but everytime when I try to use the getCell method I only recieve a NullPointerException :frowning: Im using the latest version of Libgdx and I know some of the methods has been redesigned. The map will render fine though.

package com.mayogames.zombiecubes;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.mayogames.zombiecubes.screens.GameScreen;

public class World {
	ZombieCubes zombieCubes;
	private GameScreen gameScreen;
	
	TiledMap testMap;
	OrthogonalTiledMapRenderer mapRender;
	
	TiledMapTileLayer layer;
	
	public World(ZombieCubes zombieCubes, GameScreen gameScreen, String mapName){
		this.zombieCubes = zombieCubes;
		this.gameScreen = gameScreen;
		
		if(mapName == "testMap"){
			TiledMap testMap = new TmxMapLoader().load("data/Maps/testMap.tmx");
			OrthogonalTiledMapRenderer mapRender = new OrthogonalTiledMapRenderer(testMap);
			
			layer = (TiledMapTileLayer) testMap.getLayers().get(0);
			
			System.out.println(layer.getCell(0, 0));
			
			this.layer = layer;
			this.testMap = testMap;
			this.mapRender = mapRender;
		}
		
	}
	
	public void tick(){
		
	}
	
	public void render(OrthographicCamera camera){
		mapRender.setView(camera);
		mapRender.render();
	}
	
	public TiledMapTileLayer getCollisionLayer(){
		return layer;
	}
}

I haven’t used the TiledMap at all but, could it be that when getCell() returns null it means that the cell is empty? Then all you have to do is check if it’s null and if it is, you obviously don’t have to do collisions with it so you can skip it.

e.g.

Tile t = tiledMap.getCell(x,y);
if (t != null) {
  // do collision
} else {
  // do nothing
}

I asume you get the NullPointerException at the line:

System.out.println(layer.getCell(0,0));

If that happens your layer is null. You should check your Layers and if they are actually visible or existent at all :).

To cut down on memory use, the map loader only instantiates cells for those positions which have contents at load time.