Search code examples
javaeclipselibgdxtiled

NullPointerException Libgdx


I've seen questions similar to this, but none that would match what I'm experiencing with my Libgdx project (desktop only). I've made a .tmx map using Tiled and copied it to the assets folder, which has two subpackages: gameScreens (with the maps) and tiles. If I open the .tmx map from Eclipse using Tiled it works just fine. Here's the .tmx:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="8" height="8" tilewidth="32" tileheight="32">
 <tileset firstgid="1" name="default" tilewidth="32" tileheight="32">
  <tile id="0">
   <image width="32" height="32" source="../tiles/rock.png"/>
  </tile>
  <tile id="1">
   <image width="32" height="32" source="../tiles/tile_default_1.jpg"/>
  </tile>
  <tile id="2">
   <image width="32" height="32" source="../tiles/tile_default_2.jpg"/>
  </tile>
  <tile id="3">
   <image width="32" height="32" source="../tiles/wall.png"/>
  </tile>
 </tileset>
 <layer name="Camada de Tiles 1" width="8" height="8">
  <data encoding="base64" compression="gzip">
   eJxjYWBgYCGAmXFgZHlGJHFGLPKU8okxH5f78GEAbrAA2Q==
  </data>
 </layer>
</map>

I created an abstract class GameState:

package com.mygdx.cavedive.game.GameStates;

//imports

public abstract class GameState {

    private static TmxMapLoader mapLoader = new TmxMapLoader();
    protected static OrthogonalTiledMapRenderer otmr;
    protected static OrthographicCamera cam;

    protected OrthogonalTiledMapRenderer getRenderer() {

        return otmr;

    }

    protected TmxMapLoader getMapLoader() {

        return mapLoader;

    }

    protected void disposeRenderer() {

        otmr.dispose();

    }

}

and a class that extends it:

package com.mygdx.cavedive.game.GameStates;

//imports

public class Level_1 extends GameState implements Screen {

    private TiledMap map;

    @Override
    public void render(float delta) {

        //Clear the screen
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        otmr.setView(cam);

        //Render the screen
        otmr.render();

    }

    @Override
    public void resize(int width, int height) {

        cam.viewportWidth = width;
        cam.viewportHeight = height;
        cam.update();

    }

    @Override
    public void show() {

        map = getMapLoader().load("gameScreens/level1.tmx");
        otmr = new OrthogonalTiledMapRenderer(map, 1f / 32f);
        cam = new OrthographicCamera();

    }

    @Override
    public void hide() {

        dispose();

    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {

        disposeRenderer();
        map.dispose();

    }

}

Now here's what I get:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.loadTilesets(TmxMapLoader.java:246)
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:118)
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104)
    at com.mygdx.cavedive.game.GameStates.Level_1.show(Level_1.java:40)
    at com.badlogic.gdx.Game.setScreen(Game.java:61)
    at com.mygdx.cavedive.game.App.GameCore.create(GameCore.java:28)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

It has something to do with the tilesets, but I can't figure out what.


Solution

  • The "image collection" tileset where each tile refers to its own image was a new feature in Tiled 0.10. Most likely the version of libgdx you're using does not support these kind of tilesets yet.

    According to this issue on github, libgdx nightlies do support this feature.