0

I have written Apex class and VF page.I am trying to access VF page using https://c.ap2.visual.force.com/apex/Test19 but it gives error saying 'Attempt to de-reference a null object'

Apex class:

    public class testemail {
        final Id[] contactIds;
    public testemail(ApexPages.StandardController controller) {
        contactIds = new Id[0];
        Contact[] con = [SELECT Id FROM Contact LIMIT 99];
        for(Integer index = 0; index < con.size(); index++) {
            contactIds.add(con[index].Id);
        }
    }
    public void sendmail(){
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
        mail.setTargetObjectIds(contactids);
        Messaging.sendEmail(new Messaging.MassEmailMessage[]{mail});
    }
}

VF Page (Test19):

<apex:page standardController="Contact" extensions="testemail">
    <apex:form >
    <apex:commandButton value="Send Email" action="{!sendmail}"/>
    </apex:form>
</apex:page>
1

You need to initialize a list before you can use it. However, since it's not used outside the constructor, you shouldn't place the declaration outside of the constructor. Your code should look more like this:

public class testemail {
    final Id[] contactIds;
    public testemail() {
        contactIds = new Id[0];
        Contact[] con = [SELECT Id FROM Contact LIMIT 99];
        for(Integer index = 0; index < con.size(); index++) {
            contactIds.add(record.Id);
        }
    }
    public void sendmail() {
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
        mail.setTargetObjectIds(contactids);
        Messaging.sendEmail(new Messaging.MassEmailMessage[]{mail});
    }
}
  • now i am getting error saying "SendEmail failed. First exception on row 0; first error: MASS_MAIL_LIMIT_EXCEEDED, Failed to send email: []".I have used above apex class.I am editing my code in theabove asked question. – Jack vardy Nov 7 '15 at 15:14
  • That's a different question. Namely, you're probably in a sandbox with email disabled. Check setup under email deliverability. – sfdcfox Nov 7 '15 at 16:56
1

The contactids variable has not been initialised and so is null when this expression is executed:

contactids.add

The simplest place to initialise is it is where you declare it:

private final list<id> contactids = new list<id>();

PS

If your page never displays the Contact records you don't need to use anything other than local variables to do the job. Also Map has a convenient constructor that saves you having to loop at all:

public class testemail {
    public void sendmail() {
        Map<Id,Contact> contacts = new Map<Id,Contact>([select id from contact limit 99]);
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
        mail.setTargetObjectIds(contacts.keySet());
        Messaging.sendEmail(new Messaging.MassEmailMessage[]{mail});
    }
}
  • Now i am getting error saying "List index out of bounds: 77 " – Jack vardy Nov 7 '15 at 13:37
  • @RobertBarnett You are getting less than 99 records; see sfdcfox's use of the actual size of the list or the PS code I've added. – Keith C Nov 7 '15 at 14:18
  • now i am getting error saying "SendEmail failed. First exception on row 0; first error: MASS_MAIL_LIMIT_EXCEEDED, Failed to send email: []".I have used above apex class.I am editing my code in theabove asked question. – Jack vardy Nov 7 '15 at 15:04
  • @RobertBarnett See e.g. MASS_MAIL_LIMIT_EXCEEDED - how to predict?. While you can send unlimited email to Users (people who have logins) Salesforce limits how many emails you can send to Contacts. So if you need to send more than 1,000 a day you will need to find a different approach. – Keith C Nov 7 '15 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.