1

I have seen and other relevant posts and I understand that the error is in doInBackground method moreover understands and from mistakes occurring, but I can't understand which place of code I must change or to convey onPostExecute method.If someone has some idea what's this I should change to code, thanks. The following is the code of doInBackground method etc.

protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        // Check your log cat for JSON response
        Log.d("All products:",json.toString()); //141 line

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_CLASSES);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

     return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this, productsList,
                        R.layout.list_item, new String[] { TAG_PID,
                                TAG_NAME},
                        new int[] { R.id.id, R.id.name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

}

And the following is the ERRORS

06-05 20:06:49.454: E/Buffer Error(1981): Error converting result       java.lang.NullPointerException: lock == null
06-05 20:06:49.509: E/JSON Parser(1981): Error parsing data org.json.JSONException: End of input at character 0 of 
06-05 20:06:49.584: W/dalvikvm(1981): threadid=10: thread exiting with uncaught exception (group=0xb4da3908)
06-05 20:06:49.664: E/AndroidRuntime(1981): FATAL EXCEPTION: AsyncTask #1
06-05 20:06:49.664: E/AndroidRuntime(1981): java.lang.RuntimeException: An error occurred while executing doInBackground()
06-05 20:06:49.664: E/AndroidRuntime(1981):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at jav a.lang.Thread.run(Thread.java:856)
06-05 20:06:49.664: E/AndroidRuntime(1981): Caused by: java.lang.NullPointerException
06-05 20:06:49.664: E/AndroidRuntime(1981):     at com.panos.appphpconnect.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:141)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at com.panos.appphpconnect.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-05 20:06:49.664: E/AndroidRuntime(1981):     ... 4 more
06-05 20:07:03.554: W/Trace(1981): Unexpected value from nativeGetEnabledTags: 0
3
  • You have a NullPointerException on line 141. Which line is the 141? Commented Jun 5, 2014 at 21:40
  • @eternay Log.d("All products:",json.toString()); ,I have the notes and the code Commented Jun 5, 2014 at 22:11
  • Just one comment, but it's not the cause of your problem. The method onPostExecute() of an AsyncTask is executing on the UI Thread. You don't have to creat a new Thread and execute it with runOnUIThread() method, because it's automatic with the onPostExecute() method of an AsyncTask. Commented Jun 5, 2014 at 22:21

1 Answer 1

1

Objet json is null in line 141. You have a parser error, that means that the parser can't read your object and in this case, it returns null.

Next line, you try to use the json object (json.toString()) and you obtain a NullPointerException.

Have a look at the object you try to parse on line:

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

That line returns a null object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.