2016-06-09 94 views
-2

我知道一個關於java的相當數量,但是對於Slick2d和JSON是新手。我無法弄清楚爲什麼在嘗試加載.json映射文件時拋出異常。我使用的是eclipse,slick2d,lwjgl和json。我內置的地圖文件,它位於日食下的RES /地圖/ 這裏是堆棧跟蹤JSON和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沒有發生錯誤我剛剛收到我寫的「Map does not exist」的錯誤信息如果你可以看看,這將是超級!由於

情況下你需要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); 
} 
} 
+1

你會得到什麼例外?用'System.out.println'打印例如 – Enigo

+1

只是打印出異常 –

+1

請在你的問題中加入打印堆棧。 @ squirt706謝謝 –

回答

1

有兩種例外的java介紹。

A)經過異常(編譯時間): - 處理在編譯時例外只以檢測任何外部組件或資源相關性(如FileNotFoundIOException)。

B)未經檢查的異常(運行時異常): - 此異常來到同時,因爲任何意外情況的任何邏輯錯誤或任何錯誤(IndexOutOfBoundNullPointer等)

現在您的問題

爲什麼在嘗試加載.json映射文件時拋出異常?

如果您嘗試加載在你的程序的任何文件.json這是從任何存儲區(硬盤,雲等)加載的外部資源。因此,存儲區域可能不存在File的機會。所以,爲了處理這種不確定的情況,你的程序應該準備好處理這種情況(這不是一個邏輯錯誤,它是對你的代碼的間接部分的其他資源的依賴)。

這就是把這種東西放在Checked Exception段的原因。

1

當你發現異常e時,你應該用e做一些事情。要獲得有關拋出異常的更多信息,請使用以下代碼:

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

謝謝,所以我更新了我的帖子。我查找了如何讀取堆棧跟蹤(從下到上),但無法進行任何進展World:33 is solids = parse((JSONArray)layer.get(「data」)); – squirt706

+0

而且例外情況非常明顯。 JSON解析器將'data'字段解釋爲一個字符串(因爲JSON數據看起來像它),但是您強制Java將它解釋爲JSONArray。查看JSON數據以及它的結構。 –

相關問題