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
Karunakar BuruguKarunakar Burugu 

Error while reading static List Variable

Please help me with the error detailed as in the comments section. I declared static variable and trying to add values in 'ProcessEmail" method but getting the error as explained in the comments in the code.

Also, trying to retrieve the List in Finish method, but assigned a null.
Code:
global class BatchController implements Database.Batchable<sObject>{
    
   public static List<Messaging.SingleEmailMessage> Emails;
   
    global Database.QueryLocator start(Database.BatchableContext bc){
       return Database.getQueryLocator([SELECT Id, Name, Client__r.Name, Status__c, Pri__c, NSR__c FROM scope__c
                                         WHERE Status__c IN (1,2,3) ]);  
    }
    
    global void execute(Database.BatchableContext bc, List<scope__c> scopeList){
        
        if(scopeList != NULL && scopeList.size() > 0){
            for(scope__c prc : scopeList){
                BatchController.processEmail(prc);               
            }
        }
    }
    
    global void finish(Database.BatchableContext bc){
            system.debug('Finish method '+ BatchController.Emails); // Error: This is printing null value, as this is the static variable and added values in 'processEmail' method.
              Messaging.SendEmailResult[] results = Messaging.sendEmail(BatchController.Emails, false);// Error : Internal Salesforce error
    }
    
    global static void processEmail(scope__c prc){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        /* Some Custom Email Code to fill all the mail fields */
        
       
        BatchController.Emails.add(mail); //Error: |FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
        System.debug('Process method '+ BatchController.Emails);
    } 
    
   
        
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Karunakar,

Can you please explain, what is your exact requirement?

Thanks!!
 
PriyaPriya (Salesforce Developers) 
Hey,

You have created a variable "Emails" but you have not assigned any value to it. And hence it will always display null.

Thanks!