尝试调用虚方法' int org.json.JSONArray.length()'在null对象引用上

时间:2015-10-01 10:06:41

标签: android json

尝试使用JSON数据填充列表视图时出现以下错误:

10-01 11:50:34.678    2782-2797/? I/System.out﹕ Returned Json object {"match_name":"EG vs EHOME","match_time":"2015-10-15 09:00:00"}
10-01 11:50:34.678    2782-2782/? I/System.out﹕ Resulted Value: {"match_name":"EG vs EHOME","match_time":"2015-10-15 09:00:00"}
10-01 11:50:34.679    2782-2782/? D/AndroidRuntime﹕ Shutting down VM
10-01 11:50:34.679    2782-2782/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.eduard.games, PID: 2782
java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference

在以下代码行中:

for(int i = 0; i < jsonArray.length(); i++){

我的方法将json数据添加到listview

List<getMatch> returnParsedJsonObject(String result){

    List<getMatch> jsonObject = new ArrayList<getMatch>();

    JSONObject resultObject = null;

    JSONArray jsonArray = null;

    getMatch newItemObject = null;


    for(int i = 0; i < jsonArray.length(); i++){

        JSONObject jsonChildNode;

        try {

            jsonChildNode = jsonArray.getJSONObject(i);

            String match_name = jsonChildNode.getString("match_name");

            String match_time = jsonChildNode.getString("match_time");


            newItemObject = new getMatch(match_name, match_time);

            jsonObject.add(newItemObject);

        } catch (JSONException e) {

            e.printStackTrace();

        }

    }

    return jsonObject;
}

我的json输出:

[{"match_name":"Secret vs EG","match_time":"2015-09-30 02:06:30"},     {"match_name":"LGD vs EHOME","match_time":"2015-10-08 13:10:00"},   {"match_name":"EG vs EHOME","match_time":"2015-10-15 09:00:00"}]

我将不胜感激。

2 个答案:

答案 0 :(得分:0)

很简单 你在这里

JSONArray jsonArray = null;

getMatch newItemObject = null;


for(int i = 0; i < jsonArray.length(); i++){

正在创建null json数组并在for循环中使用它的方法。首先找到JsonArray。

答案 1 :(得分:-1)

不要使null JSONarray。首先初始化