JSONArray中的NullPointerException

时间:2013-04-04 11:16:31

标签: java json

我正在阅读JSON ReST API。 JSON看起来与众不同 第一个看起来像这样:

processJSON({
  "TripList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestTrip.xsd",
  "servertime":"10:39",
  "serverdate":"2013-04-04",
  "Trip":[{
    "Leg":[{
      "name":"Buss 761",
      "type":"BUS",
      "id":"9015014576100035",
      "direction":"Heden via Linnéplatsen",
      "fgColor":"#00A5DC",
      "bgColor":"#ffffff",
      "stroke":"Solid",
      "Origin":{
        "name":"Hökegårdsgatan, Mölndal",
        "type":"ST",
        "id":"9022014012273002",
        "routeIdx":"30",
        "time":"10:42",
        "date":"2013-04-04",
        "track":"B ",
        "$":"\n"
        }

第二个像这样:

processJSON({
"TripList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestTrip.xsd",
  "servertime":"10:46",
  "serverdate":"2013-04-04",
  "Trip":[{
    "Leg":{
      "name":"Spårvagn 8",
      "type":"TRAM",
      "id":"9015014500800075",
      "direction":"Angered",
      "fgColor":"#872387",
      "bgColor":"#ffffff",
      "stroke":"Solid",
      "accessibility":"wheelChair",
      "Origin":{
        "name":"Korsvägen, Göteborg",
        "type":"ST",
        "id":"9022014003980005",
        "routeIdx":"12",
        "time":"10:50",
        "date":"2013-04-04",
        "track":"E ",
        "rtTime":"10:51",
        "rtDate":"2013-04-04",
        "$":"\n"
        }

现在我想做的是,如果JSON“Leg”是一个数组I循环进入数组并获取数据。 但是如果“Leg”是一个对象,那么循环是没有用的,我可以挖掘并获取数据。 这个对我有用

但是第一个不起作用并给出了nullpointerexpection。

我的代码是:

public class ParseTimeTable {

String temp = "";

public void parseJsonData() throws ParseException, IOException {

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(Planner.getPlanner().getJsonDataForTrip(
            Planner.getPlanner().getStartLocationID(),
            Planner.getPlanner().getDestinationID()));
    JSONObject topObject = (JSONObject) obj;
    JSONObject tripList = (JSONObject) topObject.get("TripList");
    JSONArray array = (JSONArray) tripList.get("Trip");
    Iterator<JSONObject> iterator = array.iterator();

    while (iterator.hasNext()) {

        JSONObject jsonObj = iterator.next();

        if (jsonObj.get("Leg") instanceof JSONArray)

        {

            JSONArray array2 = (JSONArray) tripList.get("Leg");
            Iterator<JSONObject> iterator2 = array2.iterator();
            while (iterator2.hasNext()) {

                String line = (String) jsonObj.get("name");
                String direction = (String) jsonObj.get("direction");
                JSONObject origin = (JSONObject) jsonObj.get("Origin");
                String name = (String) origin.get("name");
                String time = (String) origin.get("time");
                String track = (String) origin.get("track");

                JSONObject destination = (JSONObject) jsonObj.get("Destination");
                String dname = (String) destination.get("name");
                String dtime = (String) destination.get("time");
                String dtrack = (String) destination.get("track");

                temp = line + " " + "Mot : " + direction + " " + "Från : "
                        + name + " " + "Avgång : " + time + " "
                        + "Track : " + track + "- " + "Destination : "
                        + dname + " " + "Ankomst : " + dtime + " "
                        + "Track : " + dtrack;

                TripService.getTrip().setList(temp);

            }

        }

        else {

            jsonObj = (JSONObject) jsonObj.get("Leg");
            String line = (String) jsonObj.get("name");
            String direction = (String) jsonObj.get("direction");
            JSONObject origin = (JSONObject) jsonObj.get("Origin");
            String name = (String) origin.get("name");
            String time = (String) origin.get("time");
            String track = (String) origin.get("track");

            JSONObject destination = (JSONObject)                           jsonObj.get("Destination");
            String dname = (String) destination.get("name");
            String dtime = (String) destination.get("time");
            String dtrack = (String) destination.get("track");

            temp = line + " " + "Mot : " + direction + " " + "Från : "
                    + name + " " + "Avgång : " + time + " " + "Track : "
                    + track + "- " + "Destination : " + dname + " "
                    + "Ankomst : " + dtime + " " + "Track : " + dtrack;

            TripService.getTrip().setList(temp);

        }

    }

}

}

所以我的else语句中的所有内容都应该像它应该的那样工作但是在我的if语句中我得到一个nullpointer因为array2是空的.. 那么给我一个nullpointer的是我的第二个数组:JSONArray array2 =(JSONArray)tripList.get(“Leg”);

为什么会这样?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at se.lowdin.trafficPlanner.ParseTimeTable.parseJsonData(ParseTimeTable.java:35)
    at se.lowdin.trafficPlanner.MainWindow$UpdateAction.actionPerformed(MainWindow.java:294)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

在“if”语句中,您正在检查 jsonObj .get()的类型,但是您要将 tripList .get()的值分配给您的阵列。

我认为你的意思是:

JSONArray array2 = (JSONArray) jsonObj.get("Leg");

答案 1 :(得分:0)

TripList不包含Leg

在这一行:

JSONArray array2 = (JSONArray) tripList.get("Leg");

你试图从“TripList”获得“Leg”,没有

尝试使用:

JSONArray array2 = (JSONArray) jsonObj.get("Leg");