+ Start a Discussion
golugolu 

apex code issue null pointer

Hi,
Can someone please help me resolve the issue. I am getting null pointer at line 21 .         SMSHelper.createCommunicatOHistory(activityIds, message, scheduleDateTime);
public class SMSHelper {
    public static List<Task> lstTask;
    public static List<Communicat_O_activity__c> lstComActivity;
    public SMSHelper(){
        lstTask = new List<Task>();
        lstComActivity = new List<Communicat_O_activity__c>();
    } 
    
    
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        List<Id> activityIds = new List<Id>();
        if(smsConfig.Use_Salesforce_Activity__c){
            
            createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        SMSHelper.createCommunicatOHistory(activityIds, message, scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
        // SMSHelper.createCommunicatOHistory(List<id> activityIds);
    }
    
    public static void createCommunicatOHistory(List<id> activityIds, String message, Datetime scheduleDateTime){
        system.debug('lsttask' + lstTask);
        system.debug('comactivity' + lstComActivity);
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        Communicato_History__c comHistory = new Communicato_History__c();
        comHistory.Message__c = message;
        comHistory.Schedule_Date_Time__c = scheduleDateTime;
        comHistory.Activity_Ids__c = String.valueOf(activityIds);
        insert comHistory;
    }
    
    public static void createSalesforceActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Task> lstTask = new List<Task>();
        
        for(String id: recordIdSet){
            Task taskObj = new Task();
            taskObj.ActivityDate = Date.valueOf(scheduleDateTime);
            taskObj.Status = status;
            taskObj.Description = message;
            taskObj.WhoId = id;
            
            lstTask.add(taskObj); 
        }
        insert lstTask;
        
        
        // Map<Id, Task> TaskMap = tnew Map<Id, Task>(lstTask);
    }
    
    public static void createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        
    }
    
}

 
Best Answer chosen by golu
Raj VakatiRaj Vakati
Use this 
 
public class SMSHelper {
    public static List<Task> lstTask = new List<Task>(); 
    public static List<Communicat_O_activity__c> lstComActivity=new List<Communicat_O_activity__c>()
    public SMSHelper(){
    } 
    
    
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        List<Id> activityIds = new List<Id>();
        if(smsConfig.Use_Salesforce_Activity__c){
            
            createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        SMSHelper.createCommunicatOHistory(activityIds, message, scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
        // SMSHelper.createCommunicatOHistory(List<id> activityIds);
    }
    
    public static void createCommunicatOHistory(List<id> activityIds, String message, Datetime scheduleDateTime){
        system.debug('lsttask' + lstTask);
        system.debug('comactivity' + lstComActivity);
		activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        Communicato_History__c comHistory = new Communicato_History__c();
        comHistory.Message__c = message;
        comHistory.Schedule_Date_Time__c = scheduleDateTime;
        comHistory.Activity_Ids__c = String.valueOf(activityIds);
        insert comHistory;
    }
    
    public static void createSalesforceActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Task> lstTask = new List<Task>();
        
        for(String id: recordIdSet){
            Task taskObj = new Task();
            taskObj.ActivityDate = Date.valueOf(scheduleDateTime);
            taskObj.Status = status;
            taskObj.Description = message;
            taskObj.WhoId = id;
            
            lstTask.add(taskObj); 
        }
        insert lstTask;
        
        
        // Map<Id, Task> TaskMap = tnew Map<Id, Task>(lstTask);
    }
    
    public static void createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
The following line is causing the issue  .. becasue you use the activityIds in the method arguments  and not initlizeed

  public static void createCommunicatOHistory(List<id> activityIds, String message, Datetime scheduleDateTime){


change it as below 

 
public class SMSHelper {
    public static List<Task> lstTask;
    public static List<Communicat_O_activity__c> lstComActivity;
    public SMSHelper(){
        lstTask = new List<Task>();
        lstComActivity = new List<Communicat_O_activity__c>();
    } 
    
    
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        List<Id> activityIds = new List<Id>();
        if(smsConfig.Use_Salesforce_Activity__c){
            
            createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        SMSHelper.createCommunicatOHistory(activityIds, message, scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
        // SMSHelper.createCommunicatOHistory(List<id> activityIds);
    }
    
    public static void createCommunicatOHistory(List<id> activityIds, String message, Datetime scheduleDateTime){
        system.debug('lsttask' + lstTask);
        system.debug('comactivity' + lstComActivity);
		activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        Communicato_History__c comHistory = new Communicato_History__c();
        comHistory.Message__c = message;
        comHistory.Schedule_Date_Time__c = scheduleDateTime;
        comHistory.Activity_Ids__c = String.valueOf(activityIds);
        insert comHistory;
    }
    
    public static void createSalesforceActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Task> lstTask = new List<Task>();
        
        for(String id: recordIdSet){
            Task taskObj = new Task();
            taskObj.ActivityDate = Date.valueOf(scheduleDateTime);
            taskObj.Status = status;
            taskObj.Description = message;
            taskObj.WhoId = id;
            
            lstTask.add(taskObj); 
        }
        insert lstTask;
        
        
        // Map<Id, Task> TaskMap = tnew Map<Id, Task>(lstTask);
    }
    
    public static void createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        
    }
    
}

 
golugolu
Hi Raj Vataki,

it still errors out. Can you please tell me why is line 30 and 31 coming as null in debug logs? that is the issue over here.
Raj VakatiRaj Vakati
Use this 
 
public class SMSHelper {
    public static List<Task> lstTask = new List<Task>(); 
    public static List<Communicat_O_activity__c> lstComActivity=new List<Communicat_O_activity__c>()
    public SMSHelper(){
    } 
    
    
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        List<Id> activityIds = new List<Id>();
        if(smsConfig.Use_Salesforce_Activity__c){
            
            createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        SMSHelper.createCommunicatOHistory(activityIds, message, scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
        // SMSHelper.createCommunicatOHistory(List<id> activityIds);
    }
    
    public static void createCommunicatOHistory(List<id> activityIds, String message, Datetime scheduleDateTime){
        system.debug('lsttask' + lstTask);
        system.debug('comactivity' + lstComActivity);
		activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        Communicato_History__c comHistory = new Communicato_History__c();
        comHistory.Message__c = message;
        comHistory.Schedule_Date_Time__c = scheduleDateTime;
        comHistory.Activity_Ids__c = String.valueOf(activityIds);
        insert comHistory;
    }
    
    public static void createSalesforceActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Task> lstTask = new List<Task>();
        
        for(String id: recordIdSet){
            Task taskObj = new Task();
            taskObj.ActivityDate = Date.valueOf(scheduleDateTime);
            taskObj.Status = status;
            taskObj.Description = message;
            taskObj.WhoId = id;
            
            lstTask.add(taskObj); 
        }
        insert lstTask;
        
        
        // Map<Id, Task> TaskMap = tnew Map<Id, Task>(lstTask);
    }
    
    public static void createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        
    }
    
}

 
This was selected as the best answer