JSON Parser Error :Value null of type org.json.JSONObject$1 cannot be converted to JSONObject

I am getting the following error:

enter image description here

This is my source code:

package com.example.arpok.iomatic;


LineGraphSeries<DataPoint> series;
ArrayList<HashMap<String, String>> arrayListData;
public static String data;
TextView graphOneTV,graphTwoTV,graphThreeTV;
GraphView graph;
String x,y;
public Graph_Fragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_graph_, container, false);
}
HttpURLConnection connection;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    graphOneTV = (TextView) getView().findViewById(R.id.TVGraph1);
    graphTwoTV = (TextView) getView().findViewById(R.id.TVGraph2);
    graphThreeTV = (TextView) getView().findViewById(R.id.TVGraph3);
    graph = (GraphView) getView().findViewById(R.id.graph1);
    graphDataClass gdc = new graphDataClass();
    gdc.execute();


}
public class graphDataClass extends AsyncTask<String ,String ,String>
{
    @Override
    protected String doInBackground(String... strings) {

        try {
            URL url = new URL("http://192.168.1.34/getGraphData.php?");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null)
            {
                line = bufferedReader.readLine();
                data = data + line;
            }

            JSONObject jo = new JSONObject(data);

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

                x = jo.getString("twitter");
                y = jo.getString("googleplus");

                System.out.print("x: "+x+"y: "+y);

                HashMap<String, String> tempHashMapData = new HashMap<>();

                tempHashMapData.put("twitter",x);
                tempHashMapData.put("googleplus",y);

                arrayListData.add(tempHashMapData);
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    protected void onPreExecute()
    {
        super.onPreExecute();
    }
    protected  void onPostExecute(String s  )
    {
        super.onPostExecute(s);
        graphOneTV.setText(data);

    }
}}

The above code is the fragment in which I need to fetch the data form the JSON and plot it on the graph. The thing is when I am displaying the "data" named variable which is having the JSON data on a textView then it is showing properly but when I am assigning the same "data" variable to the JSONObject then the above error occurs.

This is my Json data which i need to plot on the Line Graph

[{"twitter":"200","googleplus":"60"},{"twitter":"150","googleplus":"180"},{"twitter":"90","googleplus":"120"}]

Someone please tell me how can I fetch the JSON data and parse it and plot it on a line graph.

2 answers

  • answered 2018-05-16 05:42 Hardik Vasani

    try this one.

     JSONArray jsonarray = new JSONArray(jsonStr);
     for (int i = 0; i < jsonarray.length(); i++) {
         JSONObject jsonobject = jsonarray.getJSONObject(i);
                x = jo.getString("twitter");
                y = jo.getString("googleplus");
    
                System.out.print("x: "+x+"y: "+y);
    
                HashMap<String, String> tempHashMapData = new HashMap<>();
    
                tempHashMapData.put("twitter",x);
                tempHashMapData.put("googleplus",y);
    
                arrayListData.add(tempHashMapData);
    }
    

  • answered 2018-05-16 05:43 jeet parmar

    Try this way

    ArrayList<HashMap<String, String>> arrayListSocial = new ArrayList<>();
    
    String string = "[{\"twitter\":\"200\",\"googleplus\":\"60\"},{\"twitter\":\"150\",\"googleplus\":\"180\"},{\"twitter\":\"90\",\"googleplus\":\"120\"}]";
        try {
            JSONArray jsonarray = new JSONArray(string);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
               String socailTwiter= jsonobject.getString("twitter");
                String socailGplus = jsonobject.getString("googleplus");
    
                Log.e("socailTwiter",socailTwiter);
    
                HashMap<String, String> hashMap = new HashMap<>();
    
                hashMap .put("twitter",socailTwiter);
                hashMap .put("googleplus",socailGplus );
    
    
                arrayListSocial.add(hashMap);
            }
    
        }catch (Exception e){
           e.printStackTrace();
        }