2

I am dealing with my web services and I got a strange problem and I couldn't able to solve this as I am new to android development. I am getting null pointer exception while I am showing the toast in onPostExecute() method of async task. some one please help me to fix this

if (result.booleanValue()) // here at this line I am getting the error in logcat//

Thanks in advance....!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ///////////male female buttons code/////////////
    Button regmalebtn = (Button) findViewById(R.id.regmalebtn); 
    regmalebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            gender="M";
            System.out.println("gender value after  male button click ="+gender);
            Log.v(TAG,"gender value on  male button click ="+gender );
        }
    });
    Button regfemalebtn = (Button)findViewById(R.id.regfemalebtn);
    regfemalebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            gender ="F";
            // System.out.println("gender value on  female button click ="+gender);
            Log.v(TAG,"gender value on  female button click ="+gender );
        }
    });

    ////////////male female button code will ends here/////////////

    Button signin = (Button) findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new RegisterTask().execute();
        };
    });
}


private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(Register.this);

    protected void onPreExecute() {
        this.dialog.setMessage("Registering...");
        this.dialog.show();
    }


    //  @SuppressWarnings("unused")
    protected Boolean doInBackground(final Void unused) {
        return this.register(); //don't interact with the ui!
    }

    private Boolean register() {
        ************some code for webservices************
    }

    return null;

}

protected void onPostExecute(final Boolean result) {
    if (this.dialog.isShowing()) {
        this.dialog.dismiss();
    }
    if (result.booleanValue()) {
        // show register success dialog
        // I m getting the error at above line

        Toast.makeText(Register.this, "Registerd", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(Register.this, "Try Again", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected Boolean doInBackground(Void... params) {
    // TODO Auto-generated method stub
    return register();
}


}   
}

These r the logcat errors

06-28 16:55:56.685: WARN/dalvikvm(708): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 16:55:56.695: ERROR/AndroidRuntime(708): FATAL EXCEPTION: main
06-28 16:55:56.695: ERROR/AndroidRuntime(708): java.lang.NullPointerException
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at com.soap.Register$RegisterTask.onPostExecute(Register.java:193)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at com.soap.Register$RegisterTask.onPostExecute(Register.java:1)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.AsyncTask.finish(AsyncTask.java:417)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.Looper.loop(Looper.java:123)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.app.ActivityThread.main(ActivityThread.java:4627)
3
  • xoriant.com/blog/mobile-application-development/… Commented Jun 28, 2011 at 11:40
  • how to make a null check before calling result.booleanValue()? Commented Jun 28, 2011 at 12:03
  • I tried with the below suggestions but its only showing progress dialog as output, nothing else Commented Jun 28, 2011 at 13:03

5 Answers 5

3

You are obviously returning null from register method. Either return something that isn't null or make a null check before calling result.booleanValue().

You could do that like this:

if (result == null) {

}
else if (result.booleanValue()) {

}
else {

}

By the way, why do you need to return a Boolean and not boolean (primitive type, which can't be null).

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

4 Comments

Thanks for ur response, but could you help with code snippet, I m confused a bit
how to make a null check before calling result.booleanValue()?
Thanks once again, I want to check the result of logcat if its 1 then I should show a toast or alert like "REGISTERD" Ootherwise "TRYAGAIN"
I tried but only progress dialog is showing and nothing, after few minutes force closing
0

Try using as argument of the Toast the name of your activity.class. I think in your case is Register.class instead of Register.this

2 Comments

If I use register.class I m getting error like The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (Class<Register>, String, int)
This answer is incorrect. See: developer.android.com/reference/android/widget/…, java.lang.CharSequence, int)
0

Try passing the context into variable. Or try Register.this.getApplicationContext() or BaseContext. Check if the dialog is null, make the pass the values and create new dialog in the postExecute() if that does not overlap from your requirements :)

3 Comments

Thanks, but how to check the null value in order to not return null,bit confused
@Raj Check like this if(dialog != null) { do the rest of operation }; so if the dialog is null, then nothing will happen. If the dialog is not null then operations proceed regularly.
Thanks , I got the prblm with my toast according to logcat response.In every case I m getting "Try again" if (resultsRequestSOAP == null) { Toast.makeText(Register.this.getApplicationContext(), "Try Again", Toast.LENGTH_SHORT).show(); } else if (resultsRequestSOAP.booleanValue()) { //also show register success dialog Toast.makeText(Register.this.getApplicationContext(), "Registerd", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(Register.this.getBaseContext(), "Field is empty", Toast.LENGTH_SHORT).show(); }
0

i've written this

protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

            Toast.makeText(
                    myclassname.this,
                    "Downloading of " + FILENAME
                            + " complete.", Toast.LENGTH_LONG).show();
            refreshList();


        }

and this is workning for me..

see this link for more detail
http://hi.baidu.com/hi_android/blog/item/fea149f90da7fdedfd037f56.html
http://www.screaming-penguin.com/node/7746

1 Comment

@Pragana, Thanks , I got the prblm with my toast according to logcat response.In every case I m getting "Try again" if (resultsRequestSOAP == null) { Toast.makeText(Register.this.getApplicationContext(), "Try Again", Toast.LENGTH_SHORT).show(); } else if (resultsRequestSOAP.booleanValue()) { //also show register success dialog Toast.makeText(Register.this.getApplicationContext(), "Registerd", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(Register.this.getBaseContext(), "Field is empty", Toast.LENGTH_SHORT).show(); }
0

I got rid of the same Toast/Async/Nullpointe problem by launching Toasts like this:

Toast.makeText(getApplicationContext(), "Hello Toast!", Toast.LENGTH_SHORT).show();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.