【问题标题】:JSON and Eclipse - Why is an exception being thrown?JSON 和 Eclipse - 为什么抛出异常?
【发布时间】:2016-06-09 05:36:05
【问题描述】:

我对 java 有相当多的了解,但对 Slick2d 和 JSON 还是陌生的。我无法弄清楚为什么在尝试加载 .json 地图文件时会引发异常。我正在使用 eclipse、slick2d、lwjgl 和 json。我建立了一个地图文件,它位于 eclipse 下 res/maps/ 这是堆栈跟踪

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);
}
}

【问题讨论】:

  • 你得到了什么异常?例如用System.out.println 打印它
  • 只打印出异常
  • 请在您的问题中添加打印堆栈。 @squirt706 谢谢
  • 您知道相当多的 Java,但您不知道使用 e.printStackTrace(); 显示异常堆栈跟踪的绝对基础知识?
  • @Enigo 你不要使用System.out.println 来显示异常。使用它来显示NullPointerException 将显示null,这与他当前的异常“处理”一样有用。

标签: java json eclipse lwjgl slick


【解决方案1】:

java 中引入了两种异常。

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

B) 未经检查的异常(运行时异常) :- 此异常发生在任何逻辑错误或由于任何意外情况导致的任何错误时。(IndexOutOfBoundNullPointer 等)

现在你的问题

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

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

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

这就是将这种东西放在Checked Exception Segment 中的原因。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 谢谢,所以我更新了我的帖子。我查找了如何读取堆栈跟踪(从下到上)但无法取得任何进展 World:33 is solids = parse((JSONArray)layer.get("data"));
    • 而且异常非常明显。 JSON 解析器将 data 字段解释为字符串(因为 JSON 数据看起来像),但您强制 Java 将其解释为 JSONArray。查看 JSON 数据及其结构。
    猜你喜欢
    • 2010-12-09
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2011-12-12
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多