-2

I am working on an android project and ran into problems. I have an activity which has a "Write a Review" button which opens a dialog, which in turn has two buttons "Done" and "No Thanks". The "Done" button executes an AsyncTask which stores data to server but it causes the app to crash with a NullPointerException error. Here's the code:

writeReviewBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                // Create custom dialog object
                final Dialog dialog = new Dialog(GetReviewActivity.this);
                // Include dialog.xml file
                dialog.setContentView(R.layout.write_review_activity);
                // Set dialog title
                dialog.setTitle("Your review is valuable");

                // set values for custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.tvWR);
                //text.setText("Custom dialog Android example.");
                ImageView image = (ImageView) dialog.findViewById(R.id.smiley);


                dialog.show();

                Button writeButton = (Button) dialog.findViewById(R.id.buttonWR);

                writeButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new WriteReviewAsyncTask().execute();
                    }
                });

                Button declineButton = (Button) dialog.findViewById(R.id.buttonNoThanks);
                // if decline button is clicked, close the custom dialog
                declineButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Close dialog
                        dialog.dismiss();
                    }
                });


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

The writeButton is the "Done" button. Here's the AsyncTask implementation:

public class WriteReviewAsyncTask extends AsyncTask<Void,Void,Void>{

    final EditText etWR = (EditText)findViewById(R.id.etWR);
    String Review = etWR.getText().toString();

    @Override
    protected Void doInBackground(Void... params) {
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        try {
            Log.i("HAPPENED: ","Working");
        userLocalStore = new UserLocalStore(GetReviewActivity.this);
        User user = userLocalStore.getLoggedInUser();

        dataToSend.add(new BasicNameValuePair("reviewer_name",user.username));
        dataToSend.add(new BasicNameValuePair("item_name",ItemName));
        dataToSend.add(new BasicNameValuePair("review",Review));

        HttpParams httpParams = getHttpRequestParams();
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        HttpPost httpPost = new HttpPost(SERVER_ADDRESS+"StoreReview.php");


            httpPost.setEntity(new UrlEncodedFormEntity(dataToSend));
            httpClient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        UpdateRecyclerView();
    }

    private HttpParams getHttpRequestParams() {
        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        return httpRequestParams;
    }
}

And the LogCat error:

09-23 22:12:22.110  30716-30716/app.usrete.jayant.delvemitt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at app.usrete.jayant.delvemitt.childscreen.getreviews.getreview.GetReviewActivity$WriteReviewAsyncTask.<init>(GetReviewActivity.java:330)
        at app.usrete.jayant.delvemitt.childscreen.getreviews.getreview.GetReviewActivity$2$1.onClick(GetReviewActivity.java:133)
        at android.view.View.performClick(View.java:4475)
        at android.view.View$PerformClick.run(View.java:18786)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

Please help me out guys!

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Jayant Usrete
  • 69
  • 1
  • 4
  • 13
  • 1
    It would be nice if you provided us with the exact line in witch the error happens. – Mateus Brandao Sep 23 '15 at 16:59
  • @MateusBrandao Can't say about the exact line which is causing the error. It just crashes when the "Done" button(i.e the writeButton) is clicked. I guess that there's a problem inside the `onClick()` method when the AsyncTask is being executed : `new WriteReviewAsyncTask().execute();` – Jayant Usrete Sep 23 '15 at 17:30
  • @MateusBrandao It is the empty String `Review` in the AsyncTask. I tried to move it into the `onClick` method above but still I am getting the NullPointerException. I have an an `EditText` in the custom dialog from which I am trying to retreive the typed string by user. – Jayant Usrete Sep 23 '15 at 18:48

1 Answers1

0

Why are you passing return null in protected Void doInBackground(Void... params) That passes that arguement to onPostExecute(Void params) which wont take any Argument,its type Void,so avoid writing return null,just remove that line

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • Void is a class in Java, so doInBackground must return something even null. It is not same as *void* expression. – adnbsr Sep 23 '15 at 17:21