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
FinneyFinney 

System.NullPointerException: Attempt to de-reference a null object

Hi,

 

I have written a trigger on the agreement object to create auto events when the record meets the given criteria.

 

I am getting the Null pointer error which is

 

CreateConditionReports: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger:CreateConditionReports: line 14, Column 1

 

I am posting my trigger here. Please help.

 

trigger CreateConditionReports on pb__Agreement__c (afterupdate) {

  integer i=1;

 

for (pb__Agreement__c  ag : trigger.new) {

   

Event e = newEvent();

   

    datetime myDate = ag.X1st_Condition_Inspection__c;

   

if(ag.Stage__c == 'Agreement Signed and Passed to Conveyance'&& ag.Agreement_Signed__c == true){

    e.WhatId = ag.id;

    e.OwnerId ='005G00000024wtF';

    e.Subject ='Condition Report 1';

    e.RecordTypeId ='012G0000000jequ';

    e.Description ='Condition Report 1 Event created through Sale Agreement';

    e.StartDateTime = myDate;

    e.EndDateTime = myDate.addHours(2);

    e.Unit_Number__c = ag.Unit_Number__c;

    e.Unit_Type__c = ag.pb__UnitType__c;

    e.Unit_Bedrooms__c = ag.Unit_Bedrooms__c;

    e.Tenant_Name__c = ag.pb__PrimaryContactFullName__c;

    e.Lease_Expiration_Date__c = ag.pb__LeaseExpirationDate__c;*/

    e.Agreement_Name__c = ag.Name;

   

   

insert e;

    }

  

Event e1 = newEvent();

   

    datetime myDate1 = ag.X2nd_Condition_Inspection__c;

   

if(ag.Stage__c == 'Agreement Signed and Passed to Conveyance' && ag.Property_Tenanted__c == 'No'){

    e1.WhatId = ag.id;

    e1.OwnerId ='005G00000024wtF';

    e1.Subject ='Condition Report 2';

    e1.RecordTypeId ='012G0000000jeqz';

    e1.Description ='Condition Report 2 Event created through Sale Agreement';

    e1.StartDateTime = myDate1.addHours(6);

    e1.EndDateTime = myDate1.addHours(8);

    e1.Unit_Number__c = ag.Unit_Number__c;

    e1.Unit_Type__c = ag.pb__UnitType__c;

    e1.Unit_Bedrooms__c = ag.Unit_Bedrooms__c;

    e1.Tenant_Name__c = ag.pb__PrimaryContactFullName__c;

    e.Lease_Expiration_Date__c = ag.pb__LeaseExpirationDate__c;*/

    e1.Agreement_Name__c = ag.Name;

   

   

insert e1;

    }

 

   }

  }

 

Can someone please help me.

 

Finney

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The most likely candidate looks like this line to me:

 

e.EndDateTime = myDate.addHours(2);

 

If myDate is null, then the attempt to invoke the addHours method will throw the exception you are seeing.  I'd add some debug to output the value of the field that you populate myDate from.

 

 

All Answers

bob_buzzardbob_buzzard

The most likely candidate looks like this line to me:

 

e.EndDateTime = myDate.addHours(2);

 

If myDate is null, then the attempt to invoke the addHours method will throw the exception you are seeing.  I'd add some debug to output the value of the field that you populate myDate from.

 

 

This was selected as the best answer
FinneyFinney

Thanks Bob.

 

The Date field wasn't getting populated and so the error.