JSON和Eclipse - 为什么会抛出异常?

时间:2016-06-09 05:36:06

标签: java json eclipse lwjgl slick

我对java很了解,但对Slick2d和JSON来说是新手。我无法弄清楚为什么在尝试加载.json映射文件时会抛出异常。我正在使用eclipse,slick2d,lwjgl和json。我构建了一个地图文件,它位于res / maps /下的eclipse中 这是堆栈跟踪

java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONArray
at cardboard.world.World.load(World.java:33)
at cardboard.Engine.initStatesList(Engine.java:49)
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 cardboard.Engine.main(Engine.java:31)

这是执行代码

try {
        World.load("res/maps/world.json");
    } catch (Exception e) {
        System.err.println("Map does not exist");
        System.exit(0);
    }

这是世界级

import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;

import cardboard.Resources;

public class World {

public static Image[][] solids;
public static int WIDTH;
public static int HEIGHT;

public static void load(String path) throws Exception {

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(new FileReader(path));
    JSONObject jObj = (JSONObject) obj;

    JSONArray layers = (JSONArray) jObj.get("layers");
    int amount = layers.size();

    for (int i = 0; i < amount; i++) {
        JSONObject layer = (JSONObject) layers.get(i);
        String type = (String) layer.get("name");

        if (type.equals("solids")) {
            solids = parse((JSONArray)layer.get("data"));
        } else if (type.equals("spawns")) {
            // Spawn point code
        }
    }

}

private static Image[][] parse(JSONArray arr) {

    Image[][] layer = new Image [WIDTH][HEIGHT];
    int index;

    for (int y = 0; y < WIDTH; y++) {
        for (int x = 0; x < HEIGHT; x++) {
            index = (int)((long)arr.get((y * WIDTH) + x));
            layer[x][y] = getSpriteImage(index);
        }
    }

    return layer;
}

private static Image getSpriteImage(int index) {
    if (index == 0) return null;
    index -= 1;

    SpriteSheet sheet = Resources.getSprite("tileset");
    //int vertical = sheet.getVerticalCount();
    int horizontal = sheet.getHorizontalCount();

    int y = (index / horizontal); //vert?
    int x = (index % horizontal);

    return sheet.getSubImage(x, y);
}
}

Eclipse没有丢失任何错误我只是收到错误消息我写道“地图不存在”如果你能看看它会超级!感谢

如果您需要Resources.java

package cardboard;

import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SpriteSheet;

import cardboard.world.Tile;

public class Resources {

private static Map<String, Image> images;
private static Map<String, SpriteSheet> sprites;
private static Map<String, Sound> sounds;

public Resources() {

    images = new HashMap<String, Image>();
    sprites = new HashMap<String, SpriteSheet>();
    sounds = new HashMap<String, Sound>();

    try {
        sprites.put("tileset", loadSprite("res/tileset.png",  Tile.SMALL_SIZE, Tile.SMALL_SIZE));
    } catch (SlickException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static Image loadImage(String path) throws SlickException {
    return new Image(path, false, Image.FILTER_NEAREST);
}

public static SpriteSheet loadSprite(String path, int tw, int th) throws    SlickException {
    return new SpriteSheet(loadImage(path), tw, th);
}

public static SpriteSheet getSprite(String getter) {
    return sprites.get(getter);
}

public static Image getSpriteImage(String getter, int x, int y) {
    return sprites.get(getter).getSubImage(x,y);
}

public static Image image(String getter) {
    return sprites.get(getter);
}

public static Image getImage(String getter) {
    return images.get(getter);
}

public static Sound getSound(String getter) {
    return sounds.get(getter);
}
}

2 个答案:

答案 0 :(得分:1)

java中引入了两种异常。

  

A)检查异常(编译时间): - 在编译时处理异常只是为了检测任何外部组件或资源依赖关系(如FileNotFoundIOException)。

     

B)未经检查的异常(运行时异常): - 由于任何意外情况,任何逻辑错误或任何错误都会出现此异常。(IndexOutOfBoundNullPointer等。 )

现在你的问题

  

为什么在尝试加载.json映射文件时会抛出异常?

如果您尝试在程序中加载任何文件.json,该文件是从任何存储区域(磁盘,云等)加载的外部资源。因此,存储区域可能不存在File

因此,为了处理这种不确定的情况,您的程序应该准备好处理这种情况(这不是一个逻辑错误,它依赖于其他资源,这是您的代码的间接部分)。

这就是将此类内容放入Checked Exception段的原因。

答案 1 :(得分:1)

当您捕获异常e时,您应该对e执行某些操作。要获取有关抛出异常的位置的更多信息,请使用以下代码:

} catch (Exception e) {
  e.printStackTrace();
  System.exit(1); // Because 1 means failure, 0 means OK.
}