I am getting a null pointer exception when I am iterating over a set of custom settings which I have initialised earlier. Should the exception be thrown if the set is empty but not null?
Outstanding_Product_Group_Index_Mapping__c
is a custom setting. I am following Trigger Pattern for Tidy, Streamlined, Bulkified Triggers by Tony Scott.
/*Trigger Handler for the Account SObject. This class implements the
*ITrigger interface to help ensure the trigger code is bulkified and all in one place.*/
public class Outstanding_TriggerHandler implements ITrigger{
Map<String,String> mapOfProdDivsAndGrp = new Map<String, String>();
Map<String, Set<Outstanding_Product_Group_Index_Mapping__c>> mapOfGroupAndSetting = new Map<String, Set<Outstanding_Product_Group_Index_Mapping__c>>();
Map<String,Outstanding_Product_Group_Index_Mapping__c> mapOfNameAndSetting = Outstanding_Product_Group_Index_Mapping__c.getAll();
public Outstanding_TriggerHandler() {
}
public void bulkBefore() {
if(Trigger.isInsert) {
for(Outstanding_Product_Group_Index_Mapping__c opgi : mapOfNameAndSetting.Values()) {
if(!mapOfProdDivsAndGrp.containsKey(opgi.Product_Divisions__c)) {
mapOfProdDivsAndGrp.put(opgi.Product_Divisions__c, opgi.Group_Index__c);
}
if(!mapOfGroupAndSetting.containsKey(opgi.Group_Index__c)) {
mapOfGroupAndSetting.put(opgi.Group_Index__c, new Set<Outstanding_Product_Group_Index_Mapping__c>());
}
mapOfGroupAndSetting.get(opgi.Group_Index__c).add(opgi);
}
}
}
public void bulkAfter() {
}
public void beforeInsert(SObject so) {
Outstanding__c os = (Outstanding__c)so;
String prodgrp;
Set<Outstanding_Product_Group_Index_Mapping__c> setOfSettings = new Set<Outstanding_Product_Group_Index_Mapping__c>();
if(os.Product_Division__c != NULL && os.Days_Due__c != NULL) {
for(String proddivs: mapOfProdDivsAndGrp.keySet()) {
if(proddivs.contains(os.Product_Division__c)) {
prodgrp = mapOfProdDivsAndGrp.get(proddivs);
os.Product_Group_Index__c = prodgrp;
}
}
try {
setOfSettings = mapOfGroupAndSetting.get(prodgrp);
for(Outstanding_Product_Group_Index_Mapping__c opgi : setOfSettings) {
if(os.Days_Due__c <= opgi.Due_Days_Upper_Limit__c && os.Days_Due__c > opgi.Due_Days_Lower_Limit__c) {
if(opgi.Index_Type__c == 'Higher') {
os.Higher_Approval_Level_Index__c = opgi.Approval_Index__c;
}
if(opgi.Index_Type__c == 'Lower') {
os.Lower_Approval_Level_Index__c = opgi.Approval_Index__c;
}
}
}
} catch (Exception ee) {
ExceptionLogging.addException('Outstanding Trigger Handler', ee);
}
} else {
os.Higher_Approval_Level_Index__c = NULL;
os.Lower_Approval_Level_Index__c = NULL;
}
}
public void beforeUpdate(SObject oldSo, SObject so) {
}
public void beforeDelete(SObject so) {
}
public void afterInsert(SObject so ) {
}
public void afterUpdate(SObject oldSo, SObject so) {
}
public void afterDelete(SObject so) {
}
public void andFinally() {
ExceptionLogging.saveException();
}
}
The Exception I am getting at the line for(Outstanding_Product_Group_Index_Mapping__c opgi : setOfSettings)
:
is below -
System.NullPointerException
Attempt to de-reference a null object
Class.Outstanding_TriggerHandler.beforeInsert: line 56, column 1
But I resolved the issue when I put an if condition for all the code in the try block:
if(mapOfGroupAndSetting.containsKey(prodgrp))
So I guess this line may also be causing the issue:
setOfSettings = mapOfGroupAndSetting.get(prodgrp);