Slick2D nullPointerException无法解析tilemap

时间:2015-11-09 17:08:46

标签: java nullpointerexception slick2d

我有一个问题是用Slick2D框架显示我的TileMap。

这是我的java代码:

 package main;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState {
    boolean verlassen = false;
    private TiledMap map;

    public Play(int state) {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        map = new TiledMap("res/Race.tmx");
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.render(0, 0);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isKeyDown(input.KEY_ESCAPE))
            sbg.enterState(0);
    }

    public int getID() {
        return 1;
    }
}

这就是我的TiledMap在编辑器中的样子。

    <?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="50" height="50" tilewidth="32" tileheight="32" nextobjectid="4">
 <tileset firstgid="1" name="hexagonAll_sheet" tilewidth="32" tileheight="32" tilecount="4096">
  <image source="hexagonAll_sheet.png" width="2048" height="2048"/>
  <tile id="133">
   <objectgroup draworder="index">
    <object id="0" x="0" y="0.25" width="31.75" height="32"/>
   </objectgroup>
  </tile>
 </tileset>
 <tileset firstgid="4097" name="hexagonObjects_sheet" tilewidth="32" tileheight="32" tilecount="512">
  <image source="hexagonObjects_sheet.png" width="1024" height="512"/>
 </tileset>
 <layer name="Background" width="50" height="50">
  <data encoding="base64" compression="gzip">
   H4sIAAAAAAAAC+3XwRLCMAgE0OrVv/bQ7/baUdgsIQHqcNhbmvIIjeP5PI7z33IUj8WR3UumxwbH+5Wf6V4Xc4gWhyNyfoYeZrYKODTLHR2whhVzlXC/tqMdOyztaEc72jFynI9LJMduW9R5SM8uNoQ7Np3F7R1Rc4VmapUtw8GkmkOYXer/qNVTxKFaijlQvdBTyMHMD1xTzCHWxVju4GAtI08xh7iesQQ41NoGFqsh0mGdL8qivUPp0XIHsFjXT5+F0UHXJqz96S/r3uSwWuB6ds8Ah2oxOCx7uRyWPhN3sZZpw4zjsr/HrRpAH3Y5rs8wDqoW6zPK/Lu/X+J3brkBONDdAtdpDiazBqUPo+8RWr31eC2g3yZDtmPF+zMN3xZvsh2dTuev8gH223TuECcAAA==
  </data>
 </layer>
 <objectgroup name="object">
  <object id="1" x="191.333" y="544" width="96.3333" height="31.3333"/>
 </objectgroup>
</map>

我已经读过“&#39;属性”存在问题。 Slick正试图解析。

Error Message: 
Mon Nov 09 17:58:15 CET 2015 ERROR:null
java.lang.NullPointerException
    at org.newdawn.slick.tiled.TileSet.<init>(TileSet.java:139)
    at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:661)
    at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:106)
    at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:90)
    at main.Play.init(Play.java:20)
    at main.Game.initStatesList(Game.java:22)
    at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
    at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
    at main.Game.main(Game.java:35)
Mon Nov 09 17:58:15 CET 2015 ERROR:Failed to parse tilemap
org.newdawn.slick.SlickException: Failed to parse tilemap
    at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:695)
    at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:106)
    at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:90)
    at main.Play.init(Play.java:20)
    at main.Game.initStatesList(Game.java:22)
    at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
    at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
    at main.Game.main(Game.java:35)
Caused by: java.lang.NullPointerException
    at org.newdawn.slick.tiled.TileSet.<init>(TileSet.java:139)
    at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:661)
    ... 8 more

你能帮忙解决这个错误吗?

亲切的问候

1 个答案:

答案 0 :(得分:0)

我查看了自己的tmx文件和Slick2d源文件,我真的不明白为什么你的代码不能运行。我的第一个想法是你可能没有为要加载的文件指定一些属性,但是我自己的tmx文件没有使用任何属性(还),它们运行得很好!

你可以尝试一下:

  1. 在tmx文件中添加一些属性?您不应该这样做,但如果它与属性一起运行,则至少可以解决问题
  2. 虽然Slick2d已经很老了,但现在你可能正在运行一个过时的版本。检查您正在运行的版本并在必要时进行更新。我认为build 237是最新版本
  3. 尝试制作更多tmx文件,并检查 Tiled 以检查您的版本是否过时(尽管我怀疑这是您的问题)
  4. 如果您绝望,可以下载Slick2d平铺包修改它们,以便以更安全的方式处理属性加载(但我猜测第2步将解决问题;))

我现在看到这个问题已经有10天了。所以你很可能已经找到了解决方案。如果是的话,你可以在这里发布吗?