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

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
All Answers
Thanks Simon,
You are right, that seems to be the problem, but why?. More facts to help you help me :)
trigger UpdateContactDonation on Opportunity (after insert) {
for (Opportunity Donation : Trigger.New) {
ContactUpdate.ContactOppUpdate(Donation.Contacto__r);
}
}
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
Thanks Simon. So I guess this means the workaround I found is actually the right way to accomplish this, right? Regards,
J