+ Start a Discussion
TheLearnerTheLearner 

Getting null pointer exception--very urgent

Hi Experts

Im getting this error while saving the code:

There are validations as well:

1. Actual_Start_Date:IF( AND(ISPICKVAL(Pre_Construction_Survey_Been_Carried_Out__c, 'No') ,NOT(ISBLANK( Actual_Start_Date__c ) )),true,false)

2. Estimate_Start_Date: IF( ISBLANK( NRSWA_Number__c ) , Estimated_Start_Date__c < IF(ISNULL(CreatedDate), TODAY() , (DATEVALUE(CreatedDate)))
, false)

3.Estimate_End_Date: Estimated_End_Date__c < Estimated_Start_Date__c

4. Est_Start_Date_Greater_Than_Est_End_Date:Estimated_Start_Date__c > Estimated_End_Date__c


Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CheckonNonWorkingday caused an unexpected exception, contact your administrator: CheckonNonWorkingday: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CheckonNonWorkingday: line 84, column 1;
coudl you please help me its urgent
This is the trigger:

trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update) 
{

        List<shutdown_day__c> holidayList = new List<shutdown_day__c>([Select Date__c from shutdown_day__c]);

        Set<Date> holidayDayeSet = new Set<Date>();

         

        for(shutdown_day__c holiday : holidayList)
        {

            holidayDayeSet.add(holiday.Date__c);

        }

        for(SW_Notice__c swnotice : trigger.new) 
        {

     

            if(!swnotice.Non_Working_Day__c) 
            {

                     

                if(holidayDayeSet.contains(swnotice.Estimated_Start_Date__c))
                {

                    swnotice.addError('Estimated start date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Estimated_End_Date__c))
                {

                    swnotice.addError('Estimated End date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Works_Stop__c))
                {

                    swnotice.addError('Work Stop date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Target_Stop__c))
                {

                    swnotice.addError('Target Stop date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day())))
                {

                    swnotice.addError('Actual start  date should not be a Holiday');

                }

                else if(((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if(((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E') == 'Sun')
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if(((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if(((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun') 
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    }
TheLearnerTheLearner
Hi,

Target_Stop__c and Estimated_End_Date__c these two fieds are date fields using in forumala, if i put these two in page layout its not throwing error, if i remove these two fieds its throwing error , i need to display these two fieelds with formula fields which is mentioned below. please help me out

Target_Stop_display__c(Target_Stop__c)(formula)
Estimated_End_Date_Display__c(Estimated_End_Date__c)(formula)
salesforce mesalesforce me
Hi,

You need to check the field value is null or not in every if-condition..

like
 
else if(swnotice.Target_Stop__c !=null && (((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun'))

 
Arunkumar RArunkumar R
Hi Learner,

You need to check null condition before process any values. I have updated your code, let's try this one. 
 
trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update) 
{

        List<shutdown_day__c> holidayList = new List<shutdown_day__c>([Select Date__c from shutdown_day__c]);

        Set<Date> holidayDayeSet = new Set<Date>();

         

        for(shutdown_day__c holiday : holidayList)
        {
			if(holiday.Date__c != null)
            holidayDayeSet.add(holiday.Date__c);
        }

        for(SW_Notice__c swnotice : trigger.new) 
        {

     

            if(!swnotice.Non_Working_Day__c) 
            {

                     
                if(holidayDayeSet.contains(swnotice.Estimated_Start_Date__c))
                {

                    swnotice.addError('Estimated start date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Estimated_End_Date__c))
                {

                    swnotice.addError('Estimated End date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Works_Stop__c))
                {

                    swnotice.addError('Work Stop date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Target_Stop__c))
                {

                    swnotice.addError('Target Stop date should not be a Holiday');

                }

                else if(swnotice.Actual_Start_Date__c != null && holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day())))
                {

                    swnotice.addError('Actual start  date should not be a Holiday');

                }

                else if( swnotice.Estimated_Start_Date__c != null && (((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun'))
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if( swnotice.Estimated_End_Date__c != null &&(((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun'))
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(swnotice.Actual_Start_Date__c != null && (swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E') == 'Sun'))
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if( swnotice.Target_Stop__c != null && (((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun'))
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if( swnotice.Works_Stop__c != null && (((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun'))
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    }