You need to sign in to do that
Don't have an account?
isalew
Date field error - Attempt to de-reference a null object
I built some Apex for activities (Tasks/Events) on the lead object, but the apex class fails for several records. The error message reads:
Execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object
The stack trace mentions this line:
for (Task T : l.Tasks) { //This line is the one with an issue Date ActivityDate = date.newinstance(t.ActivityDate.year(),t.ActivityDate.month(),t.ActivityDate.day());
Any ideas?
I found my error. It is related to Subhani's advice, but I found a couple articles that helped clarify the problem and solution.
On the meaning of the dereferencing error: http://www.coderanch.com/t/367369/java/java/meant-dereferencing
On how to fix this error: http://boards.developerforce.com/t5/Apex-Code-Development/Error-Attempt-to-de-reference-a-null-object/td-p/236913
The following tweak fixed my code:
All Answers
Hi,
This information may help you to solve your issue.
Trigger.New and Trigger.old both are trigger context variables. New gives you the updated values where as Old gives you the prior values of the record.
Trigger.new is available in Before Insert, After Insert, Before Update, After Update
Trigger.old is available in Before Update, After Update, Before Delete, After Delete.
Your code is trying to use Trigger.New in after delete where the Record is deleted and its Obvious that after deletion Trigger.New will have null.
The link for the above information is,
http://stackoverflow.com/questions/12596357/i-am-not-able-to-resolve-the-error-nullobject-reference-salesforce-trigger
Thanks,
Subhani,
Salesforce Certified Developer,
www.mydbsync.com
Thank you for your help Subhani, but I'm still stuck.
Why does my apex work with almost all records under the same conditions, but not with a select few?
The error occurs on AfterUpdate and AfterInsert. Is there a better way to reference this date so it doesn't create problems?
I found my error. It is related to Subhani's advice, but I found a couple articles that helped clarify the problem and solution.
On the meaning of the dereferencing error: http://www.coderanch.com/t/367369/java/java/meant-dereferencing
On how to fix this error: http://boards.developerforce.com/t5/Apex-Code-Development/Error-Attempt-to-de-reference-a-null-object/td-p/236913
The following tweak fixed my code: