引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int org.json.JSONArray.length()'

时间:2017-12-22 09:31:07

标签: java android json nullpointerexception stringbuilder

我需要从git config lfs.fetchexclude ".\DirectoryWithBigFiles\"解析json,但我收到此错误:

String builder

我不知道为什么Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference 给我带有对象的数组!

这是我的代码:

stringBuilder.toString()

有人可以帮助我吗?

JSON响应:

enter image description here

3 个答案:

答案 0 :(得分:1)

stringBuilder.toString()返回什么?使用System.out.println()

答案 1 :(得分:0)

您忘记初始化StringBuilder

首先初始化然后再使用

StringBuilder sb = new StringBuilder();

答案 2 :(得分:-1)

你的错误是String data = object.getString("data");

JSONObject mainObject = null;
    try 
    {
        mainObject = new JSONObject(stringBuilder.toString());
        JSONArray jsonArrayPDF = mainObject.getJSONArray("pdfs");

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

        JSONObject object = jsonArrayPDF.getJSONObject(i);
        Video video=new Video();
        String url = object.getString("url");

        String titolo = object.getString("titolo");
        String sottotitolo = object.getString("sottotitolo");
        String data = object.getString("data");
        //video.setId(idd4);
        System.out.println("CAZZO:"+video.getId());
        video.setPic(url);
        video.setTitolo(titolo);
        video.setSottotitolo(sottotitolo);
        video.setData(data);

        videoList.add(video);

    }

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