The code below keeps throwing a dereference null object error. I've tried making changes to make sure the item isn't empty but I am still getting this error. It is driving me insane. This code is supposed to check if the value in the BillingPostalCode field matches a value in a field called "Name" in a custom object called DMA_Value__c, if it is it should return the value from the DMA_Value__c field in the custom object to a DMA field on the account.I have looked at the other questions that are said to be duplicates, but those do not give me the answer I need. I've tried putting the suggestions into the code to catch null values. Now that I have done that I'm confused as to how it could still have this error. Maybe a better explanation of what this error message means would help.
FIRST ATTEMPT
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)){
//not sure what this field is called
//Market2__c or Market_3__c
l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
}
else if(trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)){
//blank value if new PostalCode does not match a record in the Map
l.DMA__c = NULL;
}
}
}
}
SECOND ATTEMPT
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((l.BillingPostalCode != NULL && trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode) && validBillingPostalCode.containsKey(l.BillingPostalCode)){
//not sure what this field is called
//Market2__c or Market_3__c
l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
}
else if(l.BillingPostalCode != NULL && Trigger.isInsert && !validBillingPostalCode.containsKey(l.BillingPostalCode)){
//blank value if new PostalCode does not match a record in the Map
l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
}
else if(trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)) {
//blank value if new PostalCode does not match a record in the Map
l.DMA__c = NULL;
}
}
}