error: java.lang.NullPointerException: Try invoking the virtual method 'java.lang.String java.lang.Object.toString ()' on a null object reference


This question already has an answer here:

  • What is a NullPointerException, and how do I fix it? 12 answers

i got "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference" this error when i m trying to send email.i had seen several answers to this same questions on this site,but i m not able to rectify my error.Please do help me......below is my code SendMailActivity

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Arrays;
import java.util.List;

public class SendMailActivity extends Activity {

/** Called when the activity is first created. */
String body,number,email;
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
    Bundle b = getIntent().getExtras();
    if(b!=null) {
        number = b.getString("number");
        email = b.getString("email");
        body = b.getString("otp");
    final Button send = (Button) this.findViewById(;
    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            List<String> toEmailList = Arrays.asList(email
            if(email!=null) {
                new SendMailTask(SendMailActivity.this).execute("[email protected]",
                        "*****", toEmailList, "test", body);

                //Intent intent2 = new Intent(context, Testotp.class);
                //intent2.putExtra("otp", body);
                Intent intent = new Intent(context, Testotp.class);
                Bundle b = new Bundle();

                //Inserts a String value into the mapping of this Bundle
                b.putString("number", number);
                b.putString("otp", body);




SendMailTask(here is the error)

import android.os.AsyncTask;
import android.util.Log;

import java.util.List;

public class SendMailTask extends AsyncTask {

private ProgressDialog statusDialog;
private Activity sendMailActivity;

public SendMailTask(Activity activity) {
    sendMailActivity = activity;


protected void onPreExecute() {
    statusDialog = new ProgressDialog(sendMailActivity);
    statusDialog.setMessage("Getting ready...");

protected Object doInBackground(Object... args) {
    try {
        Log.i("SendMailTask", "About to instantiate GMail...");
        publishProgress("Processing input....");
        if(args!=null) {
            GMail androidEmail = new GMail(args[0].toString(),
                       args[1].toString(), (List) args[2], args[3].toString(), args[4].toString());   

            publishProgress("Preparing mail message....");
            publishProgress("Sending email....");
            publishProgress("Email Sent.");
            Log.i("SendMailTask", "Mail Sent.");
    } catch (Exception e) {
        Log.e("SendMailTask", e.getMessage(), e);
    return null;

public void onProgressUpdate(Object... values) {


public void onPostExecute(Object result) {


Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                                 at com.example.krithihaseshadri.proj.SendMailTask.doInBackground(
                                                                                 at android.os.AsyncTask$
                                                                                 at android.os.AsyncTask$SerialExecutor$
                                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(
                                                                                 at java.util.concurrent.ThreadPoolExecutor$

Here is very good example to send Mail using AsyncTask reference taken from

Code Snippet:

public void onClick(View v) {
    final GMailSender sender = new GMailSender("[email protected]", "password");
    new AsyncTask<Void, Void, Void>() {
        @Override public Void doInBackground(Void... arg) {
            try {
                sender.sendMail("This is Subject",
                    "This is Body",
                    "[email protected]",
                    "[email protected]");
            } catch (Exception e) {
                Log.e("SendMail", e.getMessage(), e);


Happy to help again if you need more help, Accept/upvote answer if helped you it won't cost you penny!! :)