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

advertisements

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.app.Activity;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_mail);
    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(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            List<String> toEmailList = Arrays.asList(email
                    .split("\\s*,\\s*"));
            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);
                startActivity(intent);
            }

        }
    });

}

}

SendMailTask(here is the error)

import android.app.Activity;
import android.app.ProgressDialog;
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...");
    statusDialog.setIndeterminate(false);
    statusDialog.setCancelable(false);
    statusDialog.show();
}

@Override
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....");
            androidEmail.createEmailMessage();
            publishProgress("Sending email....");
            androidEmail.sendEmail();
            publishProgress("Email Sent.");
            Log.i("SendMailTask", "Mail Sent.");
        }
    } catch (Exception e) {
        publishProgress(e.getMessage());
        Log.e("SendMailTask", e.getMessage(), e);
    }
    return null;
}

@Override
public void onProgressUpdate(Object... values) {
    statusDialog.setMessage(values[0].toString());

}

@Override
public void onPostExecute(Object result) {
    statusDialog.dismiss();
}
}

Logcat

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(SendMailTask.java:39)
                                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                 at java.lang.Thread.run(Thread.java:818)


Here is very good example to send Mail using AsyncTask reference taken from https://stackoverflow.com/a/14374639/4862126

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);
            }
        }
    }.execute();

}

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