-2

I need to parse json from String builder, but I get this error:

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

I don't know why stringBuilder.toString() give me the array with objects!

Here is my code:

try {
    // HttpResponse is an interface just like HttpPost.
    //Therefore we can't initialize them
    HttpResponse httpResponse = httpClient.execute(httpPost);

    // According to the JAVA API, InputStream constructor do nothing.
    //So we can't initialize InputStream although it is not an interface
    InputStream inputStream = httpResponse.getEntity().getContent();

    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    StringBuilder stringBuilder = new StringBuilder();

    String bufferedStrChunk = null;

    while((bufferedStrChunk = bufferedReader.readLine()) != null){
        stringBuilder.append(bufferedStrChunk);
    }

    // return stringBuilder.toString();
    System.out.println("EHEH"+stringBuilder.toString());


    JSONArray mainObject = null;
    try {
        mainObject = new JSONArray(stringBuilder.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < mainObject.length(); i++) {
        JSONObject object = null;
        try {
            object = mainObject.getJSONObject(i);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            Video video=new Video();
            String url = object.getString("url");

            String titolo = object.getString("titolo");
            String sottotitolo = object.getString("sottotitolo");
            String data = object.getString("date");
            //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();
        }
    }

Can someone help me?

JSON RESPONSE:

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
oliver
  • 39
  • 2
  • 7

3 Answers3

1

What does stringBuilder.toString() return? Use System.out.println()

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Tidder Jail
  • 472
  • 7
  • 11
  • this: EHEH{"error":false,"message":"VIDEOs fetched successfully.","pdfs":[{"url":"http:\/\/www.video.mediaset.it\/bin\/199.$plit\/640x360_C_2_video_731367_videoThumbnail.jpg","titolo":"\u201c\u00c8 stata un\u2019esperienza bellissima\u201d","sottotitolo":"Dietr – oliver Dec 22 '17 at 09:35
  • This will not work, put it in a json validador, it's missing a lot of things – Tidder Jail Dec 22 '17 at 09:39
  • i cut response because it is so long – oliver Dec 22 '17 at 09:43
0

You forgot to Initialize StringBuilder.

First Initialize and then use

StringBuilder sb = new StringBuilder();
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
-1

your mistake is 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();
    }
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31