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
dev_jhdev_jh 

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

Hi all,

 

We have created a trigger on Opportunities (after insert, update and delete) that calls a function to summarize certain Opp data on the contact related to the Opp (for this we have created a Custom Contact field in the Opp rather than using the standard related list).

 

We are getting a System.NullPointerException: Attempt to de-reference a null object error and we have no idea why (the Debug Log does not shed any light, and out calls to System.Debug are not being shown there for some reason). Here is the Code of the Class up to the offending line, can you help?

 

 

public class ContactUpdate { public static void ContactOppUpdate (Contact contacto) { System.Debug('START'); Boolean Primero = TRUE; Decimal Cantidad = 0; System.Debug('ID CONTACT: ' +contacto.Id); for(Opportunity Donacion: [Select Amount, CloseDate from Opportunity Where Contacto__c=:contacto.Id AND StageName='Concretada' order by CloseDate desc]) {

The only thing we can see is that we are testing this for a Contact with no Opportunities, so the SELECT will return no rows. However that should not be a reason for an error... Any help much appreciated.

 

Thanks

 

J

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
objects related to the trigger object (typically accessed via a __r relationship method) are not automatically populated when your trigger is called, so in your trigger Donation.Contacto__r will be null.

All Answers

SuperfellSuperfell
It seems likely that contacto is null, what's the line number that gives the error ?
dev_jhdev_jh

Thanks Simon,

 

You are right, that seems to be the problem, but why?. More facts to help you help me :)

 

  • The line that causes the issue is: System.Debug('ID CONTACT: ' +contacto.Id), the first Reference to the Contacto object.
  • I am calling this Function from a trigger in Opportunities, here is the Code (note that I am passing "Contacto__r" a reference to a Contact record related to the Opportunity.

trigger UpdateContactDonation on Opportunity (after insert) {
for (Opportunity Donation : Trigger.New) {

ContactUpdate.ContactOppUpdate(Donation.Contacto__r);
}
}

 


 

Message Edited by dev_jh on 02-16-2009 10:01 AM
dev_jhdev_jh

Hi again,

 

I managed to "fix" it by passing the Contact ID from the trigger (Contact__c instead of Contact__r) and then, within the Apex Function using the ID to retreive the Contact Object.

 

Still curious as to why I can´t just pass the object itself! Thanks,

 

J

SuperfellSuperfell
objects related to the trigger object (typically accessed via a __r relationship method) are not automatically populated when your trigger is called, so in your trigger Donation.Contacto__r will be null.
This was selected as the best answer
dev_jhdev_jh

Thanks Simon. So I guess this means the workaround I found is actually the right way to accomplish this, right? Regards,

 

J