使用 JavaMail 发送邮件的 NullPointerException API

NullPointerException where send mail using JavaMail API

我在单击发送邮件按钮时收到 NulllPointerException 错误。

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

这里我使用了Java邮件库来发送邮件。 您可以从这里获取源代码:

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.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)

请提供解决方案。

您正在使用 null context at

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

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

Utils.isNetworkAvailable(mContext);

然后最终抛出 NullPointerException.

在新的后台邮件中(上下文);上下文为空

尝试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();