You need to sign in to do that
Don't have an account?
Shawn Reichner 29
Apex null pointer Issue
Awesome developers!
I have the following trigger that is operating as intended, however every now and then I receive an Null pointer exection error on line 21 of the following code. I can not trace this back to anything and the code looks to be looking for nulls but again I am not a very seasoned developer with code so I am hopign someone can help share why this error is generated every now and then.
Appreciate the help!
I have the following trigger that is operating as intended, however every now and then I receive an Null pointer exection error on line 21 of the following code. I can not trace this back to anything and the code looks to be looking for nulls but again I am not a very seasoned developer with code so I am hopign someone can help share why this error is generated every now and then.
Appreciate the help!
trigger AcctPaymentRollup on Zuora__Payment__c (after insert, after update, before delete, after unDelete) { List<Id> AccIds=new List<Id>(); If(Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete) { for(Zuora__Payment__c mst:Trigger.New){ if(mst.Zuora__Account__c !=null && mst.Zuora__AppliedInvoiceAmount__c != null && mst.Zuora__GatewayStatus__c == 'Submitted' && mst.Zuora__PaymentMethod__c != 'Check'){ AccIds.add(mst.Zuora__Account__c); } else if (mst.Zuora__Account__c != null && mst.Zuora__AppliedInvoiceAmount__c != null && (mst.Zuora__PaymentMethod__c == 'Check' || string.ValueOf(mst.Zuora__PaymentMethod__c).startsWith('ACH'))){ AccIds.add(mst.Zuora__Account__c); } } Map<Id,Account> AccMap = new Map<Id,Account>([select id,Current_Invoice_Amount_Paid__c from Account where id in:AccIds]); for(Zuora__Payment__c mst:Trigger.New) { if(AccMap != NULL && !AccMap.IsEmpty()) { AccMap.get(mst.Zuora__Account__c).Current_Invoice_Amount_Paid__c = mst.Zuora__AppliedInvoiceAmount__c; } } if(AccMap != NULL && !AccMap.IsEmpty()) { update AccMap.values(); }} If(Trigger.isDelete) { for(Zuora__Payment__c mst:Trigger.Old){ if(mst.Zuora__Account__c !=null && mst.Zuora__GatewayStatus__c == 'Submitted' && mst.Zuora__PaymentMethod__c != 'Check'){ AccIds.add(mst.Zuora__Account__c); } else if (mst.Zuora__Account__c != null && (mst.Zuora__PaymentMethod__c == 'Check' || string.ValueOf(mst.Zuora__PaymentMethod__c).startsWith('ACH'))){ AccIds.add(mst.Zuora__Account__c); } } Map<Id,Account> AccMap = new Map<Id,Account>([select id,Current_Invoice_Amount_Paid__c from Account where id in:AccIds]); for(Zuora__Payment__c mst:Trigger.Old) { if(AccMap != NULL && !AccMap.IsEmpty()) { AccMap.get(mst.Zuora__Account__c).Current_Invoice_Amount_Paid__c = AccMap.get(mst.Zuora__Account__c).Current_Invoice_Amount_Paid__c - mst.Zuora__AppliedInvoiceAmount__c; } } if(AccMap != NULL && !AccMap.IsEmpty()) { update AccMap.values(); }} }
Need of the test that AccMap contains the Key mst.Zuora__Account__c that itself can be null.
All Answers
Need of the test that AccMap contains the Key mst.Zuora__Account__c that itself can be null.
Thanks again, just trying to understand so I know fo rthe future.
Shawn
Barry Burd (Edu, writer of Java for Dummies): The && and || operators are short circuit operators. A short circuit operator is one that doesn't necessarily evaluate all of its operands.
https://users.drew.edu/bburd/JavaForDummies4/ShortCircuitEval.pdf
for(Zuora__Payment__c mst:Trigger.Old) {
if(AccMap != NULL // if NULL : no other test is done (short circuit) the global test is false.
&& !AccMap.IsEmpty()
&& mst.Zuora__Account__c != null
&& AccMap.containsKey(mst.Zuora__Account__c) // if you arrive here, you are not sure that AccMap.containsKey(mst.Zuora__Account__c) even if mst.Zuora__Account__c != null
) { // here you are sure
AccMap.get(mst.Zuora__Account__c). // this part could be null if AccMap doesn't contains the Key(mst.Zuora__Account__c)
Current_Invoice_Amount_Paid__c // null.Current_Invoice_Amount_Paid__c => error.
}
mst.Zuora__Account__c != null : could be useless if you are sure of your data.
For safety reason, you test all the parts.
Shawn