title: Android Json Paser
body: <p>Can someone please help me understand why I am getting an error.</p>
  
  <pre><code>public class tools_main2 extends AppCompatActivity {
  
      private GridView mGridView;
  
      private tools_adapter2 mGridAdapter;
      private ArrayList&lt;tools_item2&gt; mGridData;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.gridbase);
  
          mGridView = (GridView) findViewById(R.id.gridView_base);
  
          //Initialize with empty data
          mGridData = new ArrayList&lt;&gt;();
          mGridAdapter = new tools_adapter2(this, R.layout.tools_grid_view, mGridData);
          mGridView.setAdapter(mGridAdapter);
          //Start download
          new AsyncHttpTask().execute("");
      }
  
      //Downloading data asynchronously
      public class AsyncHttpTask extends AsyncTask&lt;String, Void, String&gt; {
          HttpURLConnection urlConnection;
          StringBuilder result;
  
          @Override
          protected String doInBackground(String... params) {
              result = new StringBuilder();
              urlConnection = null;
  
              try {
                  URL url = new URL("https://script.google.com/macros/s/AKfycbxW2X5zPvG1ED7opyJ7_1zbHOvHQdVGn2rUmfDdabTA8rEkGf5o/exec");
                  urlConnection = (HttpURLConnection) url.openConnection();
                  urlConnection.connect();
                  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                  BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                  String line;
                  String result = "";
                  while ((line = reader.readLine()) != null) {
                      result += line;
                  }
                  parseResult(line);
                  // Close stream
                  if (null != in) {
                      in.close();
                  }
                  return result;
  
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  assert urlConnection != null;
                  urlConnection.disconnect();
              }
              return result.toString();
          }
  
  
          @Override
          protected void onPostExecute(String result) {
              // Download complete. Lets update UI
  
              mGridAdapter.setGridData(mGridData);
          }
  
  
          String streamToString(InputStream stream) throws IOException {
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
              String line;
              String result = "";
              while ((line = bufferedReader.readLine()) != null) {
                  result += line;
              }
  
              // Close stream
              if (null != stream) {
                  stream.close();
              }
              return result;
          }
  
          /**
           * Parsing the feed results and get the list
           *
           * @param result
           */
          private void parseResult(String result) {
  
              try {
                  JSONObject response = new JSONObject(result);
                  JSONArray posts = response.optJSONArray("posts");
                  tools_item2 item;
                  for (int i = 0; i &lt; posts.length(); i++)
                  {
                      String title = posts.optJSONObject(i).getString("title");
                      String image = posts.optJSONObject(i).getString("image");
                      item = new tools_item2();
                      item.setTitle(title);
                      item.setImage(image);
                      mGridData.add(item);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
            }
         }
      }
  </code></pre>
  
  <p>error logcat</p>
  
  <pre><code>04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
  04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
  04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
  04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.json.JSONObject.&lt;init&gt;(JSONObject.java:156)
  04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.json.JSONObject.&lt;init&gt;(JSONObject.java:173)
  04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.tools.tools_main2$AsyncHttpTask.parseResult(tools_main2.java:118)
  04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.tools.tools_main2$AsyncHttpTask.doInBackground(tools_main2.java:70)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.androidtown.ezcoffee.tools.tools_main2$AsyncHttpTask.doInBackground(tools_main2.java:50)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err:     at java.lang.Thread.run(Thread.java:818)
  </code></pre>