You need to sign in to do that
Don't have an account?

System.NullPointerException: Attempt to de-reference a null object
When I try to insert/update a new Client_CC__c record, I get a really strange error. The parent Client_Form__c record that is trying to be updated has no null value in either field, and the Sent_Date__c field for the child Client_CC__c record is also not null. Any ideas on how to resolve this?
I'm getting the following error:
System.NullPointerException: Attempt to de-reference a null object: Trigger.ClientFormUpdate12: line 20, column 1
Line 20 begins the following if statement
if(updateform.Most_Recent_Email__c <= ccs.Sent_Date__c){ updateform.Most_Recent_Email__c = ccs.Sent_Date__c; update updateForms; }
Here's the entire trigger
trigger ClientFormUpdate12 on Client_CC__c (before insert, before update) { List<ID> FormIDs = new List<ID>(); List<Client_Form__c> updateForms = new List<Client_Form__c>(); For( Client_CC__c ccs : Trigger.New){ FormIDs.add(ccs.Id); } Map<ID, Client_Form__c> FormUpdateMap = new Map<ID, Client_Form__c>(); for(Client_Form__c cfc : [Select ID , Most_Recent_Email__c, Most_Recent_Good_Lead__c FROM Client_Form__c WHERE ID in:FormIDs]) { FormUpdateMap.put(cfc.ID, cfc); } for (Client_CC__c ccs : trigger.new){ Client_Form__c updateform = FormUpdateMap.get(ccs.Id); if (ccs.Form__c != null){ if(updateform.Most_Recent_Email__c <= ccs.Sent_Date__c){ updateform.Most_Recent_Email__c = ccs.Sent_Date__c; update updateForms; } } if (ccs.Good_Lead__c = true){ if(updateform.Most_Recent_Good_Lead__c <= ccs.Sent_Date__c){ updateform.Most_Recent_Good_Lead__c = ccs.Sent_Date__c; update updateForms; } } } }
As I thought, here is your problem:
13:39:28.068 (68513000)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|select ID, Most_Recent_Email__c, Most_Recent_Good_Lead__c from Client_Form__c
13:39:28.071 (71849000)|SOQL_EXECUTE_END|[10]|Rows:0
That means that the query is returning 0 rows and so FormUpdateMap has zero elements so the get statement is setting updateform to NULL and when you then try to dereference it, it throws the error that you are getting.
The Force.com explorer is a good utility to discover why a query is not returning any rows. You can download it here:
http://wiki.developerforce.com/page/ForceExplorer
If you have any questions feel free to ask, Good Luck!!
All Answers
Reading through your code I would guess that this line:
Client_Form__c updateform = FormUpdateMap.get(ccs.Id);
is returning NULL to the updateform object. My guess is that ccs.Id does not exist in the map, probably because the FormID does not exist in the Client_Form__c object. Unfortunately, the only way to positively confirm this is in the debug logs, if you want to post it here I can look through it for you and show you what is going on, other than that let me know if you have any more questions.
Here's my log, Jake.
As I thought, here is your problem:
13:39:28.068 (68513000)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|select ID, Most_Recent_Email__c, Most_Recent_Good_Lead__c from Client_Form__c
13:39:28.071 (71849000)|SOQL_EXECUTE_END|[10]|Rows:0
That means that the query is returning 0 rows and so FormUpdateMap has zero elements so the get statement is setting updateform to NULL and when you then try to dereference it, it throws the error that you are getting.
The Force.com explorer is a good utility to discover why a query is not returning any rows. You can download it here:
http://wiki.developerforce.com/page/ForceExplorer
If you have any questions feel free to ask, Good Luck!!
Cool!
I figured the problem was that I assumed the ID was the lookup relationship. Rather, it was Form__c.
What it looks like now:
Good Deal! I'm glad you got it working.