简体   繁体   English

Slick2D nullPointerException无法解析tilemap

[英]Slick2D nullPointerException failed to parse tilemap

i have a problem to display my TileMap with Slick2D framework. 我在用Slick2D框架显示我的TileMap时遇到问题。

This is my java code: 这是我的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;
    }
}

This is what my TiledMap looks like in Editor. 这就是我的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>

I already read that there is a problem with the 'properties' which Slick is trying to parse. 我已经读过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

Can you help to fix this error ? 您能帮忙解决此错误吗?

Kind Regards 亲切的问候

I've looked through my own tmx files and the Slick2d source files, and I really don't get why your code won't run. 我浏览了自己的tmx文件和Slick2d源文件,但我真的不明白为什么您的代码无法运行。 My first thought was that you maybe HAD to specify some properties for the file to load, but my own tmx files don't use any properties (yet), and they run just fine! 我的第一个想法是,您可能已经为要加载的文件指定了一些属性,但是我自己的tmx文件尚未使用任何属性(还可以),它们运行得很好!

Could you try: 您可以尝试:

  1. Adding some properties to your tmx file? 在您的tmx文件中添加一些属性? You shouldn't have to, but if it runs with properties you have at least isolated the problem 您不必这样做,但是如果它与属性一起运行,则至少可以隔离问题
  2. Although Slick2d is quite old now it's possible you are running an outdated version. 尽管Slick2d现在已经很老了,但您可能正在运行一个过时的版本。 Check wich version you are running and update if necessary. 检查您正在运行的版本,并在必要时进行更新。 I think build 237 is the latest version 我认为237版是最新版本
  3. Try to make some more tmx files, and check Tiled to check if the version you have is outdated (although I doubt that is your problem) 尝试制作更多的tmx文件,并检查Tiled以检查您拥有的版本是否过时(尽管我怀疑那是您的问题)
  4. If you are desperate you can download the Slick2d tiled package modify them so property loading is handled in a safer way (But I'm guessing step 2 will solve the problem ;) ) 如果您不顾一切,可以下载Slick2d平铺程序包,对其进行修改,以便以更安全的方式处理属性加载(但我猜步骤2将解决问题;))

I see this question is 10 days old now.. So it's quite possible you've already found a solution. 我现在看到这个问题已有10天了。所以您很有可能已经找到了解决方案。 If so, could you post it here? 如果是这样,您可以在这里发布吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM