-1

Can someone please help me understand why I am getting an error.

public class tools_main2 extends AppCompatActivity {

    private GridView mGridView;

    private tools_adapter2 mGridAdapter;
    private ArrayList<tools_item2> 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<>();
        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<String, Void, String> {
        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 < 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();
            }
          }
       }
    }

error logcat

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.<init>(JSONObject.java:156)
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:     at org.json.JSONObject.<init>(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)
buczek
  • 1,979
  • 6
  • 27
  • 39
  • It looks like this line: `JSONArray posts = response.optJSONArray("posts");` is returning a null value. Looking at the URL for your json I am not seeing any value for "posts". – buczek Apr 26 '16 at 18:52
  • Thank you so much i have solved ha Thank you!! – H.Jeong hee Apr 27 '16 at 06:57

1 Answers1

0

The parameter you pass to the method parseResult() is empty or null.

So the JsonObject "response" and JsonArray "posts" are null. Trying to get length of an null array results in the exception.

Try to add some Null Checks or Logs to verify it.

This post might be helpful for you

Community
  • 1
  • 1