function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocio VivancoRocio Vivanco 

'System.NullPointerException: Attempt to de-reference a null object' when running a test class for a batch email job

Hi all,

when trying to run my test class, this fails with the error:
 
System.NullPointerException: Attempt to de-reference a null object

This is pointing to the code line in my apex class "String [] toEmailAddress = new String [] {er.ToEmailAddress__c};":
 
global void sendEmail(){

        Messaging.reserveSingleEmailCapacity(2);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        // Strings to hold the email addresses to which you are sending the email.
        Email_Reminder__c er = Email_Reminder__c.getInstance('List');
        String [] toEmailAddress = new String [] {er.ToEmailAddress__c};
        String replyToAddress = er.replytoAddress__c;
        String SenderDisplayName= er.SenderDisplayName__c;

  ...

​and to one line before the Test.startTest() begins. This is my test class:
 
@isTest
public class EmailReminder_Test {
    static testMethod void testMethod() {
       
      insert new Email_Reminder__c(Name =='TestCS',
                                                         ToEmailAddress__c='test@email.com', 
                                                         replytoAddress__c = 'test@email.com', 
                                                         Subject__c = 'Test Email Reminder',
                                                         SenderDisplayName__c = 'Test Sender', Body__c = 
                                                         'This is a test email reminder');

      Test.startTest();
      EmailReminder sendEmailJob = new EmailReminder();
      String sch = '00 30 20 5 2 ? *';
      system.schedule('sendEmailJob', sch, sendEmailJob);
      Test.stopTest();
    
    }
    
}
I tried to fix it, but can not handle this til now. Any tips?
 
Best Answer chosen by Rocio Vivanco
v varaprasadv varaprasad
Change name once =  Name ='List'
 
Email_Reminder__c emailremin =  new Email_Reminder__c(Name =='List',
                                                         ToEmailAddress__c='test@email.com', 
                                                         replytoAddress__c = 'test@email.com', 
                                                         Subject__c = 'Test Email Reminder',
                                                         SenderDisplayName__c = 'Test Sender', Body__c = 
                                                         'This is a test email reminder');
		insert emailremin;

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 

All Answers

v varaprasadv varaprasad
Hi Rocio,

please check once following code : 
 
@isTest
public class EmailReminder_Test {
    static testMethod void testMethod() {
       
      Email_Reminder__c emailremin =  new Email_Reminder__c(Name =='TestCS',
                                                         ToEmailAddress__c='test@email.com', 
                                                         replytoAddress__c = 'test@email.com', 
                                                         Subject__c = 'Test Email Reminder',
                                                         SenderDisplayName__c = 'Test Sender', Body__c = 
                                                         'This is a test email reminder');
		insert emailremin;

      Test.startTest();
      EmailReminder sendEmailJob = new EmailReminder();
      String sch = '00 30 20 5 2 ? *';
      system.schedule('sendEmailJob', sch, sendEmailJob);
      Test.stopTest();
    
    }
    
}


Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Rocio VivancoRocio Vivanco
Thanks varaprasad, but this is still not working after corrected as you suggested :(. The error is pointing still to the same code line in my apex class and to same code line in my test class.
I think this is because the ToEmailAddress variable is declared as array and my test class is expecting a value in an array, but I can't still handle it.
v varaprasadv varaprasad
Change name once =  Name ='List'
 
Email_Reminder__c emailremin =  new Email_Reminder__c(Name =='List',
                                                         ToEmailAddress__c='test@email.com', 
                                                         replytoAddress__c = 'test@email.com', 
                                                         Subject__c = 'Test Email Reminder',
                                                         SenderDisplayName__c = 'Test Sender', Body__c = 
                                                         'This is a test email reminder');
		insert emailremin;

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
This was selected as the best answer
Rocio VivancoRocio Vivanco
Thank you very much!!!. You are a hero :).