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
DJP1SDJP1S 

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;					 
			}
		}
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

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

Jake GmerekJake Gmerek

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.

 

 

DJP1SDJP1S

Here's my log, Jake.

 

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
13:39:28.062 (62799000)|EXECUTION_STARTED
13:39:28.062 (62838000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
13:39:28.062 (62865000)|CODE_UNIT_STARTED|[EXTERNAL]|01qc000000001SG|ClientFormUpdate12 on Client_CC trigger event BeforeUpdate for [a0h40000004ZZKt]
13:39:28.064 (64234000)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>()
13:39:28.064 (64290000)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>()
13:39:28.064 (64482000)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>()
13:39:28.064 (64520000)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>()
13:39:28.064 (64669000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Client_CC__c>.iterator()
13:39:28.064 (64999000)|SYSTEM_METHOD_EXIT|[5]|LIST<Client_CC__c>.iterator()
13:39:28.065 (65040000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
13:39:28.065 (65077000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
13:39:28.065 (65185000)|SYSTEM_METHOD_ENTRY|[6]|LIST<Id>.add(Object)
13:39:28.065 (65230000)|SYSTEM_METHOD_EXIT|[6]|LIST<Id>.add(Object)
13:39:28.065 (65241000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
13:39:28.065 (65255000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
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
13:39:28.071 (71911000)|SYSTEM_METHOD_ENTRY|[10]|Database.QueryLocator.iterator()
13:39:28.072 (72025000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
13:39:28.072 (72052000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
13:39:28.072 (72114000)|SYSTEM_METHOD_EXIT|[10]|Database.QueryLocator.iterator()
13:39:28.072 (72134000)|SYSTEM_METHOD_ENTRY|[10]|Database.QueryLocatorIterator.hasNext()
13:39:28.072 (72158000)|SYSTEM_METHOD_EXIT|[10]|Database.QueryLocatorIterator.hasNext()
13:39:28.072 (72188000)|SYSTEM_METHOD_ENTRY|[16]|LIST<Client_CC__c>.iterator()
13:39:28.072 (72228000)|SYSTEM_METHOD_EXIT|[16]|LIST<Client_CC__c>.iterator()
13:39:28.072 (72241000)|SYSTEM_METHOD_ENTRY|[16]|system.ListIterator.hasNext()
13:39:28.072 (72254000)|SYSTEM_METHOD_EXIT|[16]|system.ListIterator.hasNext()
13:39:28.072 (72298000)|SYSTEM_METHOD_ENTRY|[18]|MAP.get(ANY)
13:39:28.072 (72342000)|SYSTEM_METHOD_EXIT|[18]|MAP.get(ANY)
13:39:28.072 (72472000)|SYSTEM_METHOD_ENTRY|[20]|Exception.setMessage(String)
13:39:28.072 (72508000)|SYSTEM_METHOD_EXIT|[20]|Exception.setMessage(String)
13:39:28.072 (72618000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.ClientFormUpdate12: line 20, column 1
13:39:28.072 (72636000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.ClientFormUpdate12: line 20, column 1
13:39:28.890 (72682000)|CUMULATIVE_LIMIT_USAGE
13:39:28.890|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 6 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

13:39:28.890|CUMULATIVE_LIMIT_USAGE_END

13:39:28.072 (72731000)|CODE_UNIT_FINISHED|ClientFormUpdate12 on Client_CC trigger event BeforeUpdate for [a0h40000004ZZKt]

 

Jake GmerekJake Gmerek

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!!

This was selected as the best answer
DJP1SDJP1S

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:

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.Form__c);
			}
			
	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.Form__c);
			
			if (ccs.Form__c != null){
				if(updateform.Most_Recent_Email__c != ccs.Sent_Date__c){
					updateform.Most_Recent_Email__c = ccs.Sent_Date__c;
					updateForms.add(updateform);			
				}
			}			
			
			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;
	}
}

 

Jake GmerekJake Gmerek

Good Deal!  I'm glad you got it working.