Search code examples
libgdx

Mapping parsed json to java object not working-LibGdx


First time I am using json parsing in my LibGDX project and I am not able to get the parsed results properly.

Here I want to manage the obstacles with properties;name,direction,gap and speed.

I have a json file like this:

"obstacles": [
 {"name":"eagle","dir":"up","gap":"100","speed":"0"},
 {"name":"bigbird","dir":"up","gap":"200","speed":"0"},
 {"name":"elephant","dir":"middle","gap":"300","speed":"0"},
 {"name":"tiger","dir":"middle","gap":"400","speed":"0"},
 {"name":"bear","dir":"down","gap":"500","speed":"0"},

],

and I am reading the array like this:

public class ReadJson {
static int levelNum = 0;

public ReadJson()
    {
    }

    public static synchronized ArrayList<String> loadLevelJson(int levelno, String name) {
        ArrayList obs = new ArrayList<String>();


        JsonValue jsonValue = new JsonReader().parse(Gdx.files.internal("levels/level1.json"));
        JsonValue objList = jsonValue.get(name);
        System.out.println("name:" + objList);
        if (name.equals("obstacles")) {
            Constants.OBS_COUNT = 0;
            for (JsonValue values : objList.iterator()) // iterator() returns a
                                                            // list of children
            {
                obs.add(values.getString("dir"));
                Constants.OBS_POSITION[obs.size() - 1] = Integer.parseInt(values.getString("gap"));
                Constants.OBS_NAME[obs.size() - 1] = values.getString("name");
                Constants.OBS_SPEED[obs.size() - 1] = Integer.parseInt(values.getString("speed"));
                Constants.OBS_COUNT++;
            }
        }

        else if (.....)) {
            //code
        }
        return obs;
    }

}

Arrays are defined in constant class like this:

public static int OBS_COUNT =20;
public static int[] OBS_POSITION= new int[50];
public static String[] OBS_NAME= new String[50];
public static int[] OBS_SPEED= new int[50];
public static ArrayList<String> obsArray;

I want to map the parsed json to the object bear.For that,I did like this:

private ReadJson readJson;
public ObsObjectFactory() {
    readJson = new ReadJson();
    readJson.loadLevelJson(1,"obstacles");

    Constants.obsArray=readJson.loadLevelJson(1,"obstacles");

}
public Bear createBear() {

    Bear bear = new Bear();

    bear.setName(Constants.OBS_NAME[Constants.OBS_COUNT]);
    bear.setDirection(Constants.obsArray.get(Constants.OBS_COUNT));
    bear.setSpeed(Constants.OBS_SPEED[Constants.OBS_COUNT]);
    bear.setGap(Constants.OBS_POSITION[Constants.OBS_COUNT]);
    bear.setPosition(Constants.EAGLE_X,Constants.EAGLE_Y);
    bear.setSize(Constants.BEAR_WIDTH,Constants.BEAR_HEIGHT);
    return bear;
}
}

But when I call this createBear()method and run this code,it is throwing IndexOutOfBoundsException in this line:

bear.setDirection(Constants.obsArray.get(Constants.OBS_COUNT));

Parsed values are displaying properly in console.

What mistake I did here?


Solution

  • Your constants don't seem to be constants, as they can change. I found that odd, and as another aside, the code is quite confusing to follow. I'm not entirely sure what you're trying to achieve.

    But the index error is likely caused by incrementing your constant (hint, not a constant then, is it?) Constants.OBS_COUNT++; in the readJson method. Given your json file, OBS_COUNT might end up being 5, so Constants.obsArray.get(Constants.OBS_COUNT) is failing as your array only has index positions 0 through 4.