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
isalewisalew 

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?

Best Answer chosen by Admin (Salesforce Developers) 
isalewisalew

 

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:

 

for (Task T : l.Tasks) {

    Date ActivityDate = null;
                    
    if (t.ActivityDate != null) {
        ActivityDate = date.newinstance(t.ActivityDate.year(),t.ActivityDate.month(),t.ActivityDate.day());                    
}

 

 

All Answers

Subhani PSubhani P

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

isalewisalew

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?

isalewisalew

 

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:

 

for (Task T : l.Tasks) {

    Date ActivityDate = null;
                    
    if (t.ActivityDate != null) {
        ActivityDate = date.newinstance(t.ActivityDate.year(),t.ActivityDate.month(),t.ActivityDate.day());                    
}

 

 

This was selected as the best answer