NullPointerException使用JavaMail API发送邮件

时间:2015-01-14 07:45:53

标签: android nullpointerexception javamail

我点击按钮发送邮件时出现NulllPointerException错误。

我尝试了许多解决方案,比如调试,检查所有定义的变量,而不是提供null。

这里我使用JavaMail Library发送邮件。 你可以从这里得到这个源代码:

https://github.com/kristijandraca/BackgroundMailLibrary

以下是源代码

Java文件

package com.example.piyush_society_demo;

import com.kristijandraca.backgroundmaillibrary.BackgroundMail;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Add_Vendor_New_Activity extends Activity{
Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_vendor_activity);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        ActionBar ab = getActionBar();
        ab.setTitle("Add vender");


        Button btn_vendor_add = (Button) findViewById(R.id.btn_vendor_add);

        btn_vendor_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                send_email(
                        "Thank you for Register with Us",
                        "Hello Sir, \n\n You are successfully registered with Us.",
                        "abc@gmail.com");

            }
        });

    }

    public void send_email(String msg, String body, String email) {
        // TODO Auto-generated method stub
        BackgroundMail bm = new BackgroundMail(context);
        bm.setGmailUserName("Gmail Username");
        // "DoE/GTiYpX5sz5zmTFuoHg==" is crypted "password"
        bm.setGmailPassword("Gmail Password");
        // bm.setGmailPassword(Utils.encryptIt("chhavi2014"));
        // bm.setGmailPassword(Utils.decryptIt("mygmailaccount5"));
        bm.setMailTo(email);
        bm.setFormSubject(msg);
        bm.setFormBody(body);

        bm.setSendingMessage("Loading...");
        bm.setSendingMessageSuccess("Your message was sent successfully.");
        bm.setProcessVisibility(false);
                //bm.setAttachment(Environment.getExternalStorageDirectory().getPath()+File.pathSeparator+"somefile.t    xt");
        bm.send();

    }


}

Gmail ID已更改。

以下是Logcat

01-14 07:30:11.289: E/AndroidRuntime(7581): FATAL EXCEPTION: main
01-14 07:30:11.289: E/AndroidRuntime(7581): java.lang.NullPointerException
01-14 07:30:11.289: E/AndroidRuntime(7581):     at com.kristijandraca.backgroundmaillibrary.Utils.isNetworkAvailable(Utils.java:22)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at com.kristijandraca.backgroundmaillibrary.BackgroundMail.send(BackgroundMail.java:84)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at com.example.piyush_society_demo.Add_Vendor_New_Activity.send_email(Add_Vendor_New_Activity.java:58)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at com.example.piyush_society_demo.Add_Vendor_New_Activity$1.onClick(Add_Vendor_New_Activity.java:32)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at android.view.View.performClick(View.java:4084)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at android.view.View$PerformClick.run(View.java:16966)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at android.os.Handler.handleCallback(Handler.java:615)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at android.os.Looper.loop(Looper.java:137)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at java.lang.reflect.Method.invoke(Method.java:511)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 07:30:11.289: E/AndroidRuntime(7581):     at dalvik.system.NativeStart.main(Native Method)

请给我解决方案。

2 个答案:

答案 0 :(得分:0)

您正在使用BackgroundMail null

初始化context个对象
BackgroundMail bm = new BackgroundMail(context);

context成员变量从未初始化。然后,此null上下文将从BackgroundMail#send()方法传递到

Utils.isNetworkAvailable(mContext);

然后最终抛出NullPointerException

答案 1 :(得分:0)

在新的BackgroundMail(上下文)中; context为null

尝试BackgroundMail bm = new BackgroundMail(getApplicationContext);BackgroundMail bm = new BackgroundMail(this);

如果在onCreate

中初始化上下文,也可以修复它
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_vendor_activity);
context = getApplicationContext();