-2

I know a fair amount about java but am new to Slick2d and JSON. I cannot figure out why an exception is being thrown when trying to load the .json map file. I am using eclipse, slick2d, lwjgl and json. I built a map file and it is located in eclipse under res/maps/ Here is the stack trace

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)

Here is the executing code

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

Here is the World class

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 is throwing no errors I am just getting the error message I wrote "Map does not exist" It would be super if you could take a look! Thanks

In case you need 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);
}
}
LW001
  • 2,452
  • 6
  • 27
  • 36
squirt706
  • 33
  • 7

2 Answers2

1

there are two kind of exception introduced in java.

A) Checked Exception (Compile Time) :- Handle the exception at compile time just to detect any external component or resource dependency (like FileNotFound, IOException).

B) Unchecked Exception (Runtime Exception) :- This exception came while any logical mistake or any error because of any unexpected situation.(IndexOutOfBound, NullPointer etc.)

Now Your Question

why an exception is being thrown when trying to load the .json map file ?

If you try to load any file .json in your program which is a external resource loaded from any Storage area (disk, cloud etc). So, there may be a chance of File is not present at the storage area.

So, to handle these kind of uncertain situation your program should be ready to handle these kind of situation (this is not a logical error it's a dependency on other resource which is indirect part of Your Code).

this is the reason to put this kind of stuff in Checked Exception Segment.

Community
  • 1
  • 1
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
1

When you catch the exception e, you should do something with that e. To get more information about where the exception was thrown, use this code:

} catch (Exception e) {
  e.printStackTrace();
  System.exit(1); // Because 1 means failure, 0 means OK.
}
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Thanks, So I updated my post. I looked up how to read a stack trace (bottom to top) but cannot make any headway World:33 is solids = parse((JSONArray)layer.get("data")); – squirt706 Jun 09 '16 at 17:08
  • And the exception is very clear. The JSON parser interprets the `data` field as a string (because the JSON data looks like it), yet you force Java to interpret it as a JSONArray. Look at the JSON data and how it is structured. – Roland Illig Jun 09 '16 at 21:01