Libtiled java v1.2.3: NullPointer on accessing tiles

Dear all,
I’m creating a little multiplayer 2D game in Java using Tiled maps. I use libgdx on the client side, which loads and renders maps perfectly, but since I don’t want to use libgdx on the server side, I access maps via the libtiled v1.2.3 found on maven central.
Here is my test code:

    MapReader mapReader = new MapReader();
    Map tiledMap = null;
    try {
        tiledMap = mapReader.readMap("rooms/top.tmx");
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (tiledMap == null) {
        System.out.println("map is null");
        return;
    }
    else {
        System.out.println("map = " + tiledMap);
    }

    TileLayer ground = null;

    for (MapLayer layer : tiledMap.getLayers()) {
        if (layer.getName().equals("ground") && layer instanceof TileLayer) {
            ground = (TileLayer) layer;
        }
    }

    if (ground == null) {
        System.out.println("ground is null");
        return;
    }
    else {
        System.out.println("ground = " + ground);
    }

    System.out.println("ground empty? " + ground.isEmpty());

Output:

map = Map[16x16x3][16x16]
ground = org.mapeditor.core.TileLayer@8297b3a
Exception in thread “main” java.lang.NullPointerException
at org.mapeditor.core.TileLayer.isEmpty(TileLayer.java:240)

The NPE occurs on the line with isEmpty()

Did I do something wrong?

No, in fact the NPE occurs in the isEmpty() function, in TileLayer.java line 240:

Now, I don’t immediately see anything wrong with the above implementation (the p thing seems to be an optimization that causes it to find an answer with only half the checks in common cases), but apparently something caused a mismatch between the allocated tileMap array and the width and/or height members.

Maybe you can debug it further?

Yep, that’s what I meant. :slight_smile:

The Tile[][] tileMap field from TileLayer points to null. Interestingly, I still get the correct CSV encoded data with TileLayer::getData::getValue, and width/height are also correct.

I’m not familiar with the JAXB unmarshaller, so I couldn’t figure out where this array should be instanciated and filled up.
Do you think it could be caused by my project being run with JDK 1.8?

Finally found out that it was the use of MapReader that caused me this pain, using TMXMapReader instead solved the problem. Is MapReader deprecated?