I keep getting the System.NullPointerException: Attempt to de-reference a null object on my trigger at line 21. I've attempted to add != null to that line and I get an error too. Please see the code below.
trigger UpdateDMAAccount on Account (before insert, before update) {
//get set of Postal Codes from leads to be inserted or updated
Set<String> BillingpostalCodes = new Set<String>();
for(Account l : trigger.new){
if(l.BillingPostalCode != null){
BillingpostalCodes.add(l.BillingPostalCode);
}
}
if(BillingpostalCodes.size() > 0){
//create Map of custom object records with Name (PostalCode) as key
Map<String,DMA_Value__c> validBillingPostalCode = new Map<String,DMA_Value__c>();
for(DMA_Value__c obj : [SELECT Id, Name, DMA_Value__c FROM DMA_Value__c WHERE Name IN :BillingpostalCodes]){
validBillingPostalCode.put(obj.Name,obj);
}
for(Account l : trigger.new){
if((Trigger.isInsert || trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode) && validBillingPostalCode.containsKey(l.BillingPostalCode)){
l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
}
else if(trigger.oldMap.get(l.Id).BillingPostalCode != NULL || l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)){
//blank value if new PostalCode does not match a record in the Map
l.DMA__c = Null;
}
}
}
}
BillingPostalCodes.size() > 0
- if 0, then map is null and second condition fails on line 21; also, use of variablel
for accounts is not best practice as it makes one think of leads