java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference

时间:2018-07-24 10:23:52

标签: json nullpointerexception long-integer

Every time I open my app it crashes because of this error I dont know what should I do please help me... I have also attached logcat screen shot below and the java class in which the error is comming.

DataParser.java:

public class DataParser {

    private HashMap<String,String> getPlace(JSONObject googlePlacesJson){
        HashMap<String,String> googlePlaceMap=new HashMap<>();

        String placeName="-NA-";
        String vicinity="-NA-";
        String latitude="";
        String logitude="";
        String reference="";
        List<HashMap<String,String>> placesList=new ArrayList<>();


        try {
        if(!googlePlacesJson.isNull("name")){

                placeName=googlePlacesJson.getString("name");

        }
        if (!googlePlacesJson.isNull("vicinity")){
            vicinity=googlePlacesJson.getString("vicinity");
        }
        latitude=googlePlacesJson.getJSONObject("geometry").getJSONObject("location").getString("lat");
        logitude=googlePlacesJson.getJSONObject("geometry").getJSONObject("location").getString("lng");
        reference=googlePlacesJson.getString("reference");

        googlePlaceMap.put("name",placeName);
        googlePlaceMap.put("vicinity",vicinity);
        googlePlaceMap.put("lat",latitude);
        googlePlaceMap.put("lng",logitude);
        googlePlaceMap.put("reference",reference);




        } catch (JSONException e) {
            e.printStackTrace();
        }

        return googlePlaceMap;
    }

    private List<HashMap<String,String>> getPlaces(JSONArray jsonArray){
        long count;
       count=jsonArray.length(); // error comes on this line
        List<HashMap<String,String>> placesList=new ArrayList<>();
        HashMap<String,String> placeMap=null;

        for(int i=0;i<count;i++){
            try {
                placeMap=getPlace((JSONObject) jsonArray.get(i));
                placesList.add(placeMap);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return placesList;
    }

    public List<HashMap<String,String>> parse(String jsonData){
        JSONArray jsonArray=null;
        JSONObject jsonObject;

        try {
            jsonObject=new JSONObject(jsonData);
            jsonArray=jsonObject.getJSONArray("results");


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return getPlaces(jsonArray);

    }

}

this is the screen shot my my log cat:

enter image description here

2 个答案:

答案 0 :(得分:0)

问题出在这里:

public List<HashMap<String,String>> parse(String jsonData){
    JSONArray jsonArray=null;
    JSONObject jsonObject;

    try {
        jsonObject=new JSONObject(jsonData);
        jsonArray=jsonObject.getJSONArray("results"); // !!! this line here !!!


    } catch (JSONException e) {
         e.printStackTrace();
    }

    return getPlaces(jsonArray);

}

getJSONArray("results");返回null,这以后会引起问题(NullPointerException)。

您应该做什么:

检查JSON是否存在语法错误。 将断点放置在getJSONArray()行,并查看jsonDatajsonObject(如果一切正常)。 您要检索的数组可能无效,或者根本不在jsonData中。

答案 1 :(得分:0)

我将首先在运行整个方法之前验证jsonArray是否为空。或:

if (jsonArray != null) count=jsonArray.length(); else count = 0;

然后进行调试以找出为什么使用空对象调用该方法的原因。