0

This code is implemented to send email from an android client and it keeps crashing. When I checked the error log, it reads: I have tried troubleshooting to see where exactly this error is coming from I have not been able to see. Kindly help out.

  10-02 16:15:14.536    7493-7493/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ken4ward.emailsender, PID: 7493
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ken4ward.emailsender/com.example.ken4ward.emailsender.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5021)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.ken4ward.emailsender.MainActivity.onCreate(MainActivity.java:46)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5021)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
            at dalvik.system.NativeStart.main(Native Method)

This is the code.

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//import android.support.v7.app.AppCompatActivity;

public class MainActivity extends Activity implements View.OnClickListener{

    EditText fieldEmail, emailSubject, emailBody;
    String strFieldEmail, strEmailSubject, strEmailBody;
    Button buttonLogin;
    Context context = null ;
    Session session = null;
    ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressDialog = new ProgressDialog(MainActivity.this);
        context = this;

        fieldEmail = (EditText)this.findViewById(R.id.editEmail);
        emailSubject = (EditText)this.findViewById(R.id.editSubject);
        emailBody = (EditText)this.findViewById(R.id.editBody);

        buttonLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        strFieldEmail = fieldEmail.getText().toString();
        strEmailSubject = emailSubject.getText().toString();
        strEmailBody = emailBody.getText().toString();

        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");

        session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("kehindeadeoya@gmail.com", "321g201115@...Ademnew");
            }
        });
        progressDialog = ProgressDialog.show(context, "", "Sending email", true);
        ReceivedFeedTask receivedFeedTask = new ReceivedFeedTask();
        receivedFeedTask.execute();
    }

    class ReceivedFeedTask extends AsyncTask<String, Void, String>
    {
        @Override
        protected String doInBackground(String... params) {
            try
            {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("kehindeadeoya@gmail.com"));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strFieldEmail));
                message.setSubject(strEmailSubject);
                message.setContent(strEmailBody, "text/html; charset=utf-8");
                Transport.send(message);

            }catch (MessagingException messagingException)
            {
                messagingException.printStackTrace();
            }catch (Exception exception)
            {
                exception.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result)
        {
            progressDialog.dismiss();
            fieldEmail.setText("");
            emailSubject.setText("");
            emailBody.setText("");
            Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_LONG).show();
        }

    }
}
  • 1
    shift progressDialog = new ProgressDialog(this); inside onCreate....also I would suggest instead of 'this' keyword use 'MainActivity.this' – Shadow Droid Oct 02 '15 at 13:36
  • @ShadowDroid there's no reason to use `MainActivity.this` syntax outside of anonymous classes – maciekjanusz Oct 02 '15 at 13:46
  • 1
    @ShadowDroid The answer you've linked is kinda misleading. The `OuterClass.this` syntax will work everywhere, but is **necessary** only in nested classes, to access the reference to their outer classes. Still, no need to use this syntax in `onCreate` because it will be the same reference as if you just wrote `this`. PS. In the previous comment I meant **nested** classes, not just anonymous classes. Sorry for that – maciekjanusz Oct 02 '15 at 14:06
  • 1
    @invariant you are right...i misquoted it – Shadow Droid Oct 02 '15 at 14:09
  • I have re-edited the code to reflect the suggestions from the forum and the error persists. I checked the manifest file and it seems intact as expected because there's no other activity created than the Main. – Kehinde Adeoya Oct 02 '15 at 14:23
  • You never initialized `buttonLogin`, though this is not the problem that you initially posted, and was solved by answers posted. – Mike M. Aug 17 '17 at 00:54

3 Answers3

1

Need proper initialize ProgressDialog progressDialog ; And Call progressDialog in your oncreate() .

Actually Your Locat throws

Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103) at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143) at android.app.AlertDialog.(AlertDialog.java:98) at android.app.ProgressDialog.(ProgressDialog.java:77)

Try this way ,I hope it will help you .

public class MainActivity extends Activity implements View.OnClickListener{

EditText fieldEmail, emailSubject, emailBody;
String strFieldEmail, strEmailSubject, strEmailBody;
Button buttonLogin;
Context context = null ;
Session session = null;
ProgressDialog progressDialog ; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progressDialog = new ProgressDialog(MainActivity.this);
    fieldEmail = (EditText)findViewById(R.id.editEmail);
    emailSubject = (EditText)findViewById(R.id.editSubject);
    emailBody = (EditText)findViewById(R.id.editBody);

    buttonLogin.setOnClickListener(this);
}
IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
  • 2
    A little explanation for this: This happens because ProgressDialog is initialized *before* the class is ready to instantiate it. The right place to initialize any GUI object is during (or after) onCreate(). – HappyCactus Oct 02 '15 at 13:48
  • @HappyCactus Yes yes .Thanks – IntelliJ Amiya Oct 02 '15 at 13:54
  • 1
    @HappyCactus you're right, and that is because until `onCreate` is called, the activity object is not a valid `Context`. – maciekjanusz Oct 02 '15 at 14:11
  • I have re-edited the code to reflect the suggestions from the forum and the error persists. I checked the manifest file and it seems intact as expected because there's no other activity created than the Main. – Kehinde Adeoya Oct 02 '15 at 14:23
0

You have to add the activity in your manifest file. This CAN be a problem.

Deval Khandelwal
  • 3,187
  • 1
  • 24
  • 37
0

There is a silly mistake you did.

Initially you have declared

ProgressDialog progressDialog = new ProgressDialog(this);

and again you've initialized here

progressDialog = ProgressDialog.show(context, "", "Sending email", true);

So just remove this from

ProgressDialog progressDialog = new ProgressDialog(this);

to

ProgressDialog progressDialog;
Piyush
  • 23,959
  • 6
  • 36
  • 71
  • Who have downvoted? Just tell me – Piyush Oct 02 '15 at 13:46
  • I have re-edited the code to reflect the suggestions from the forum and the error persists. I checked the manifest file and it seems intact as expected because there's no other activity created than the Main. – Kehinde Adeoya Oct 02 '15 at 14:24