'An error occurred while executing doinbackground

An error occurred while executing doinBackground().

class PostComment extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AddComment.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        //postData("deepika");
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String post_title = intime.getText().toString();
        String post_message = outtime.getText().toString();
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(AddComment.this);
        String post_username = sp.getString("username", "anon");
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", post_username));
            params.add(new BasicNameValuePair("intime", post_title));
            params.add(new BasicNameValuePair("outtime", post_message));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL, "POST",
                    params);
            //JSONObject json1 = jsonParser.makeHttpRequest("http://192.168.10.30/webservice/comments.php", "POST",
                //  params);
            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
        if (success == 1) {     

                Log.d("Attendence Marked!", json.toString());    
                //finish();
                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("Attendence Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        finish();
        if (file_url != null) {
            Toast.makeText(AddComment.this, file_url, Toast.LENGTH_LONG).show();
        }
    }
} 

This is the error log:

11-05 05:03:20.171: E/AndroidRuntime(3171): FATAL EXCEPTION: AsyncTask #1
11-05 05:03:20.171: E/AndroidRuntime(3171): Process: com.example.mysqltest, PID: 3171
11-05 05:03:20.171: E/AndroidRuntime(3171): java.lang.RuntimeException: An error occurred while executing doInBackground()
11-05 05:03:20.171: E/AndroidRuntime(3171): at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-05 05:03:20.171: E/AndroidRuntime(3171): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-05 05:03:20.171: E/AndroidRuntime(3171): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-05 05:03:20.171: E/AndroidRuntime(3171): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-05 05:03:20.171: E/AndroidRuntime(3171): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-05 05:03:20.171: E/AndroidRuntime(3171): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-05 05:03:20.171: E/AndroidRuntime(3171): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-05 05:03:20.171: E/AndroidRuntime(3171): at java.lang.Thread.run(Thread.java:841)
11-05 05:03:20.171: E/AndroidRuntime(3171): Caused by: java.lang.NullPointerException



Solution 1:[1]

in click listener do this

 mSubmit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View vw) {
                    String post_title = intime.getText().toString();
                    String post_message = outtime.getText().toString();

                    new PostComment(post_title,post_message).execute();
                }
            }); 



and in the asynch task change this
class PostComment extends AsyncTask<String, String, String> {

            String response = "";
            String post_title ="";
            String post_message="";
            // Check for success tag
            int success;


            public PostComment(String title,String msg) {
                post_title = title;
                post_message = msg;
            }

      @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddComment.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            //postData("deepika");
        }



     @Override
        protected String doInBackground(String... args) {


            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(AddComment.this);
            String post_username = sp.getString("username", "anon");
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", post_username));
                params.add(new BasicNameValuePair("intime", post_title));
                params.add(new BasicNameValuePair("outtime", post_message));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL, "POST",
                        params);
                //JSONObject json1 = jsonParser.makeHttpRequest("http://192.168.10.30/webservice/comments.php", "POST",
                    //  params);
                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);

    if (success == 1) {

                    Log.d("Attendence Marked!", json.toString());    

                    response = json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Attendence Failure!", json.getString(TAG_MESSAGE));
                    response = json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }

            return response;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();

            if (file_url != null) {
                Toast.makeText(AddComment.this, file_url, Toast.LENGTH_LONG).show();
            }


        }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1