1

I am writing an app that sends an e-mail with multiple image as attachments. I followed this link : Android multiple email attachments using Intent to set up the Email intent and stuff .But the app force closes when I click on the send button and there seeems to be a Null Pointer Exception.

Here's the code for the two classes that are involved (only for the email part) :

PrevEmail.java :

   public class PrevEmail extends Activity{

Context context;
TextView tv,tvEmail,tvEmailAd,tvSubject,tvContent,tvLocation;
EditText etSubject,etContent,etLocation;
String subject,content,location;
Button send;
String btnSelected;
String[] imgPath;
Bundle extras;
ArrayList<Uri> uris;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prevemail);
    intializeVar();
}

private void intializeVar() {
    // Links xml to java
    tv = (TextView) findViewById(R.id.tv);
    tvEmail = (TextView) findViewById(R.id.tvEmail);
    tvEmailAd = (TextView) findViewById(R.id.tvEmailAd);
    tvSubject = (TextView) findViewById(R.id.tvSubject);
    tvContent = (TextView) findViewById(R.id.tvContent);
    tvLocation = (TextView) findViewById(R.id.tvLocation);

    etSubject = (EditText) findViewById(R.id.Subject);
    etContent = (EditText) findViewById(R.id.Content);
    etLocation= (EditText) findViewById(R.id.Location);

    send = (Button) findViewById(R.id.bSend);



    extras = getIntent().getExtras();
    if(extras!=null){
        btnSelected=extras.getString("Button");
        imgPath=extras.getStringArray("Path");

        //Sets the E-mail field according to the value received by the Bundle.
        if(btnSelected.equalsIgnoreCase("case1")){
            tvEmailAd.setText("email_address");
        }else if(btnSelected.equalsIgnoreCase("case2")){
            tvEmailAd.setText("email_address");
        }else if(btnSelected.equalsIgnoreCase("case3")){
            tvEmailAd.setText("email_address");
        }else if(btnSelected.equalsIgnoreCase("case4")){
            tvEmailAd.setText("email_address");
        }

    }   



    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            convertEditTextToString();
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"email_address");
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,content);

            uris = new ArrayList<Uri>();

            for(String file : imgPath) {
                File fileIn = new File(file);
                Uri u = Uri.fromFile(fileIn);
                uris.add(u);
            }

            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }
    });

}

    private void convertEditTextToString(){

        subject = etSubject.getText().toString();
        content = etContent.getText().toString();
        location= etLocation.getText().toString();
    }       
  }

Android manifest :

    <?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.name.StartScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>    
    </activity>
    <activity
        android:name="com.example.name.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.name.MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
         android:name="com.example.name.PrevEmail"
         android:label="@string/app_name">     
     </activity>
</application>

And the Logcat :

    12-02 21:44:48.180: W/dalvikvm(29329): threadid=1: thread exiting with uncaught   exception (group=0x40bc4498)
    12-02 21:44:48.180: E/test(29329): Exception
    12-02 21:44:48.200: E/AndroidRuntime(29329): FATAL EXCEPTION: main
    12-02 21:44:48.200: E/AndroidRuntime(29329): java.lang.NullPointerException
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at com.example.name.PrevEmail$1.onClick(PrevEmail.java:100)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at android.view.View.performClick(View.java:4106)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at  android.view.View$PerformClick.run(View.java:17150)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at android.os.Handler.handleCallback(Handler.java:615)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at android.os.Handler.dispatchMessage(Handler.java:92)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at android.os.Looper.loop(Looper.java:137)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at android.app.ActivityThread.main(ActivityThread.java:4792)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
    12-02 21:44:48.200: E/AndroidRuntime(29329):    at dalvik.system.NativeStart.main(Native Method)
3
  • 1
    I think that we don't need that much code to track an NPE. Please post the relevant parts only. Commented Dec 2, 2013 at 16:33
  • Did you try debugging your code at the position where the null pointer is coming ? You should Commented Dec 2, 2013 at 16:36
  • 1
    When you have a nullpointer, all we need to see is your logcat, and the code the logcat is pointing to. This is a bit much to sift through. Commented Dec 2, 2013 at 16:46

3 Answers 3

3

You never initialize

Context context;

So when doing context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));, it throws a NPE.

Logcat messages are very helpful for debugging. This line :

at com.example.name.PrevEmail$1.onClick(PrevEmail.java:100)

indicates that something is'nt initialized correctly on the line 100 of your activity (inside the onClick method to be precise).

Try to learn from logcat, that will save you a lot of time.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that. I changed that line to startActivity(emailIntent) and it works ! But could you please tell me how could I've implemented the context.startActivity() method ? Thanks again :)
@user2125722 You could initialize context with context = this in your onCreate() method.
@ZouZou Thanks again ! But then, what's the difference between startActivity() and context.startActivity() ?
@user2125722 There's no difference, because you're in an activity, which inherit from the context class. So you can call direclty this method.
@user2125722, ok good! :) Be sure to take a look at my answer, I believe you have another bug.
2

@ZouZou I believe has the right answer. BUT I see another potential NullPointer; You will force close when your editText are empty when you call this method:

   convertEditTextToString();

You will need to check for null on those first. What I did here was make your method return bool, and if the edit text in question are NOT null, then proceed.

  @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(convertEditTextToString()){
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"email_address");
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,content);

        uris = new ArrayList<Uri>();

        for(String file : imgPath) {
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }

        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }
   }
 });



private boolean convertEditTextToString(){

    subject = etSubject.getText().toString();
    content = etContent.getText().toString();
    location= etLocation.getText().toString();
 if(!subject.equals("") && !content.equals("") && !location.equals(""){
         return true;
  } else {
       Toast.makeText(context, "Please fill out all edit text boxes", Toast.LENGTH_LONG).show();
       return false;
   }
}       

Comments

1
send.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        convertEditTextToString();
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"email_address");
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,content);

        uris = new ArrayList<Uri>();

        for(String file : imgPath) {
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }

        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }
});

Somewhere in this code, some value is coming null. Eclipse debugger is awesome. You should debug your code.

By doing that you will find the error and solution yourself only. Do tell us which is line 100 in this class also

1 Comment

I will keep that in mind, thank you ! Found the error, as pointed out by ZouZou..:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.