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
Shri BEShri BE 

System.Null Pointer Exception in Trigger

Hi All,

I am getting system.null pointer exception: attempt to de-reference a null object, when trying to insert a record with null value in date field.
How to resolve this error. Need your assistance to fix this issue.

Below is my Trigger:
Trigger:


trigger insertDailyPractices on Study_Plan__c (after insert) 
{
    System.debug('--- Inside Trigger ---');
    List<Daily_Practice__c> dplist = new List<Daily_Practice__c>();    
    
    for(Study_Plan__c sp : Trigger.New)
    {
            Integer daycount = sp.Phase_Start_Date__c.daysBetween(sp.Phase_End_Date__c); ----/// Getting error in this line.
            
System.debug('--- Inside For Loop ---' + sp.Phase_Start_Date__c);
            System.debug('--- Day Count ---' + daycount);
     
        for(integer i=0; i<=daycount; i++)
        {
            Daily_Practice__c dps = new Daily_Practice__c();
            dps.Due_Date__c = sp.Phase_Start_Date__c.addDays(i) + 1;
            dps.StudyPlan__c = sp.Id;
            dps.Status__c = 'Assigned';
            dplist.add(dps);
        }
    }
    
    if(dplist.size() > 0)
    {
        insert dplist;
    }    
    system.debug('--- Inserted Daily Practice ---'+ dplist.size());
}

Thanks
Best Answer chosen by Shri BE
Abdul KhatriAbdul Khatri
Hi Shri,

I have just tried to cover your issues with null checks which is part of coding consideration
trigger insertDailyPractices on Study_Plan__c (after insert) 
{
    List<Daily_Practice__c> dplist = new List<Daily_Practice__c>();    
    
    for(Study_Plan__c sp : Trigger.New)
    {
        Integer daycount;
        
        if(sp.Phase_Start_Date__c != null && sp.Phase_End_Date__c != null)
        	daycount = sp.Phase_Start_Date__c.daysBetween(sp.Phase_End_Date__c); ----/// Getting error in this line.

		if(daycount == null) continue;
                
        for(integer i=0; i<=daycount; i++)
        {
            Daily_Practice__c dps = new Daily_Practice__c();
            
            if(sp.Phase_Start_Date__c != null)
            	dps.Due_Date__c = sp.Phase_Start_Date__c.addDays(i) + 1;
            
            dps.StudyPlan__c = sp.Id;
            dps.Status__c = 'Assigned';
            dplist.add(dps);
        }
    }
    
    if(dplist.size() > 0)
    {
        insert dplist;
    }    
}

 

All Answers

Andrew GAndrew G
Simplest thing to do would be to check that the fields you are using in the object are not blank:
 
trigger insertDailyPractices on Study_Plan__c (after insert) 
{
    System.debug('--- Inside Trigger ---');
    List<Daily_Practice__c> dplist = new List<Daily_Practice__c>();    
    
    for(Study_Plan__c sp : Trigger.New)
    {
        if(String.isNotBlank(sp.Phase_Start_Date__c) && String.isNotBlank(sp.Phase_End_Date__c) {

            Integer daycount = sp.Phase_Start_Date__c.daysBetween(sp.Phase_End_Date__c);
            
            System.debug('--- Inside For Loop ---' + sp.Phase_Start_Date__c);
            System.debug('--- Day Count ---' + daycount);
     
            for(integer i=0; i<=daycount; i++)
            {
                Daily_Practice__c dps = new Daily_Practice__c();
                dps.Due_Date__c = sp.Phase_Start_Date__c.addDays(i) + 1;
                dps.StudyPlan__c = sp.Id;
                dps.Status__c = 'Assigned';
                dplist.add(dps);
            }    
        }
    }
    
    if(dplist.size() > 0)
    {
        insert dplist;
    }    
    system.debug('--- Inserted Daily Practice ---'+ dplist.size());
}

HTH
Andrew G

 
Shri BEShri BE
Hi Andrew,

Thanks for your reply.

after updating the code as suggested am getting below error. Can you look and let me know.

Method does not exist or incorrect signature: void isNotBlank(Date) from the type String at line 8 column 19
 
Abdul KhatriAbdul Khatri
Hi Shri,

I have just tried to cover your issues with null checks which is part of coding consideration
trigger insertDailyPractices on Study_Plan__c (after insert) 
{
    List<Daily_Practice__c> dplist = new List<Daily_Practice__c>();    
    
    for(Study_Plan__c sp : Trigger.New)
    {
        Integer daycount;
        
        if(sp.Phase_Start_Date__c != null && sp.Phase_End_Date__c != null)
        	daycount = sp.Phase_Start_Date__c.daysBetween(sp.Phase_End_Date__c); ----/// Getting error in this line.

		if(daycount == null) continue;
                
        for(integer i=0; i<=daycount; i++)
        {
            Daily_Practice__c dps = new Daily_Practice__c();
            
            if(sp.Phase_Start_Date__c != null)
            	dps.Due_Date__c = sp.Phase_Start_Date__c.addDays(i) + 1;
            
            dps.StudyPlan__c = sp.Id;
            dps.Status__c = 'Assigned';
            dplist.add(dps);
        }
    }
    
    if(dplist.size() > 0)
    {
        insert dplist;
    }    
}

 
This was selected as the best answer
Shri BEShri BE
Hi Abdul Khatri,

Thanks for your help.. It is working fine.