0

I have created a Email service which saves attachments into opportunites, when sending emails an error message is returned back to the mailbox stating that it has failed processing because Attempt to de-reference a null object.

It says the error occured in line the where the for loop is started (line 4).

Here is the code:

global class myHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
....
    // Save binary attachments, if any
    for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
      Attachment attachment = new Attachment();

      attachment.Name = bAttachment.fileName;
      attachment.Body = bAttachment.body;
      attachment.ParentId = matcher.group(0);
      insert attachment;
    }

EDIT

Created new code to check first if there are any uploads...

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
    for (integer i = 0 ; i < email.binaryAttachments.size() ; i++)
    {          
      Attachment attachment = new Attachment();
      attachment.Name = email.binaryAttachments[i].filename;
      attachment.Body = email.binaryAttachments[i].body;
      attachment.ParentId = matcher.group(0);
      insert attachment;          
    }
}

It does not return any errors when saving however it does not save the attachments either.

3 Answers 3

1

Based on my experience, the error is not caused by the code you pasted. Since this error happened at the for (...) line, I would say most probably the object email is null. Hence when you try to retrieve email.binaryAttachments, the error occurs. You can probably debug from this.

1

Is it possible that the email.binaryAttachments collection is not initialised if there is no attachment? So that instead of being zero-length it is null?

1
  • It could possibly, I have updated the code to first check if there are uploads. Although this does not return any errors... the attachments are not saved. Also when I sent test messages, all contained attachments so the error should not have been displayed in those instances even if it is not initialised on the conditions that you mentioned. Aug 19, 2014 at 12:22
1

Okay so I have figured it out, it turns out that my email service in the settings had Attachment support disabled, hence probably the null values.

None the lesss this code should be the working code as it will first check, regardless of setting if attachments exists so no errors are spit out

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
    for (integer i = 0 ; i < email.binaryAttachments.size() ; i++)
    {          
      Attachment attachment = new Attachment();
      attachment.Name = email.binaryAttachments[i].filename;
      attachment.Body = email.binaryAttachments[i].body;
      attachment.ParentId = matcher.group(0);
      insert attachment;          
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .