You need to sign in to do that
Don't have an account?
James Sheridan
Getting "cannot de-reference null object" when I try to create a new record.
In my last for loop (last 4-5 lines of code that I post here) I get a cannot de-reference null object error when I try to insert an AHU record into the database. I've gone over the code a couple of times, and can't figure out what object I don't instantiate.
trigger updateGatewayCounts on AHU__c (after update, after delete, after insert) {
AHU__c editedAHU = new AHU__c();
if (Trigger.isDelete) {editedAHU = trigger.old[0];}
else {editedAHU = trigger.new[0];}
Map<Control_Gateway__c, Integer[]> cggwCounts = new Map<Control_Gateway__c, Integer[]>();
List<AHU__c> project_AHUs = [SELECT AHU_Project__c, AHU_Site__c, Name, AHU_Control_Group__c
FROM AHU__c
WHERE AHU_Project__c = :editedAHU.AHU_Project__c
ORDER BY Name];
for (AHU__c ahu: project_AHUs) {
List<AHU_Point__c> points = [SELECT AHU_Point_Source__c, AHU_Point_Type__c, AHU_Point_Gateway__c FROM AHU_Point__c WHERE AHU_Point_AHU__c =: ahu.Id];
if (points.size() > 0) {
Map<String, AHU_Point__c[]> sensorMap = new Map<String, AHU_Point__c[]>();
for (AHU_Point__c point: points) {
AHU_Point__c[] sensorList = sensorMap.get(point.AHU_Point_Source__c);
if (sensorList == null) {sensorList = new List<AHU_Point__c>();}
sensorList.add(point);
sensorMap.put(point.AHU_Point_Source__c, sensorList);
}
for (AHU_Point__c[] hwList: sensorMap.values()) {
Integer temperature = 0;
Integer voltage = 0;
Integer int_temp = 0;
Integer other = 0;
String gw = '';
String source = '';
for (AHU_Point__c point: hwList) {
List<Point__c> port = [SELECT Point_Port__c FROM Point__c WHERE Point_Name__c =: point.AHU_Point_Type__c];
if (port.size() > 0) {
if (port[0].Point_Port__c == 'Temperature') {temperature += 1;}
else if (port[0].Point_Port__c == 'Voltage') {voltage += 1;}
else if (port[0].Point_Port__c == 'Internal Temperature') {int_temp += 1;}
}
else {other += 1;}
gw = point.AHU_Point_Gateway__c;
source = point.AHU_Point_Source__c;
}
List<Control_Gateway__c> cggw = [SELECT CGGW_WAM_Count__c, CGGW_WSM_AHU_Count__c, CGGW_WSM_Power_Count__c
FROM Control_Gateway__c
WHERE CGGW_Project__c = :editedAHU.AHU_Project__c AND CGGW_Gateway__c = :gw AND CGGW_Control_Group__c = :ahu.AHU_Control_Group__c];
Integer[] counts = cggwCounts.get(cggw[0]);
if (counts == null) {counts = new List<Integer>();counts.add(0);counts.add(0);counts.add(0);}
if (source == 'WAM') {counts.set(0, counts.get(0)+1);}
else if (source != 'BACnet') {if (voltage > 0) {counts.set(1, counts.get(1)+1);} else {counts.set(2, counts.get(2)+1);}}
cggwCounts.put(cggw[0], counts);
}
}
}
for (Control_Gateway__c cggw: cggwCounts.keySet()) {
cggw.CGGW_WAM_Count__c = cggwCounts.get(cggw).get(0);
cggw.CGGW_WSM_Power_Count__c = cggwCounts.get(cggw).get(1);
cggw.CGGW_WSM_AHU_Count__c = cggwCounts.get(cggw).get(2);
upsert(cggw);
}
}
trigger updateGatewayCounts on AHU__c (after update, after delete, after insert) {
AHU__c editedAHU = new AHU__c();
if (Trigger.isDelete) {editedAHU = trigger.old[0];}
else {editedAHU = trigger.new[0];}
Map<Control_Gateway__c, Integer[]> cggwCounts = new Map<Control_Gateway__c, Integer[]>();
List<AHU__c> project_AHUs = [SELECT AHU_Project__c, AHU_Site__c, Name, AHU_Control_Group__c
FROM AHU__c
WHERE AHU_Project__c = :editedAHU.AHU_Project__c
ORDER BY Name];
for (AHU__c ahu: project_AHUs) {
List<AHU_Point__c> points = [SELECT AHU_Point_Source__c, AHU_Point_Type__c, AHU_Point_Gateway__c FROM AHU_Point__c WHERE AHU_Point_AHU__c =: ahu.Id];
if (points.size() > 0) {
Map<String, AHU_Point__c[]> sensorMap = new Map<String, AHU_Point__c[]>();
for (AHU_Point__c point: points) {
AHU_Point__c[] sensorList = sensorMap.get(point.AHU_Point_Source__c);
if (sensorList == null) {sensorList = new List<AHU_Point__c>();}
sensorList.add(point);
sensorMap.put(point.AHU_Point_Source__c, sensorList);
}
for (AHU_Point__c[] hwList: sensorMap.values()) {
Integer temperature = 0;
Integer voltage = 0;
Integer int_temp = 0;
Integer other = 0;
String gw = '';
String source = '';
for (AHU_Point__c point: hwList) {
List<Point__c> port = [SELECT Point_Port__c FROM Point__c WHERE Point_Name__c =: point.AHU_Point_Type__c];
if (port.size() > 0) {
if (port[0].Point_Port__c == 'Temperature') {temperature += 1;}
else if (port[0].Point_Port__c == 'Voltage') {voltage += 1;}
else if (port[0].Point_Port__c == 'Internal Temperature') {int_temp += 1;}
}
else {other += 1;}
gw = point.AHU_Point_Gateway__c;
source = point.AHU_Point_Source__c;
}
List<Control_Gateway__c> cggw = [SELECT CGGW_WAM_Count__c, CGGW_WSM_AHU_Count__c, CGGW_WSM_Power_Count__c
FROM Control_Gateway__c
WHERE CGGW_Project__c = :editedAHU.AHU_Project__c AND CGGW_Gateway__c = :gw AND CGGW_Control_Group__c = :ahu.AHU_Control_Group__c];
Integer[] counts = cggwCounts.get(cggw[0]);
if (counts == null) {counts = new List<Integer>();counts.add(0);counts.add(0);counts.add(0);}
if (source == 'WAM') {counts.set(0, counts.get(0)+1);}
else if (source != 'BACnet') {if (voltage > 0) {counts.set(1, counts.get(1)+1);} else {counts.set(2, counts.get(2)+1);}}
cggwCounts.put(cggw[0], counts);
}
}
}
for (Control_Gateway__c cggw: cggwCounts.keySet()) {
cggw.CGGW_WAM_Count__c = cggwCounts.get(cggw).get(0);
cggw.CGGW_WSM_Power_Count__c = cggwCounts.get(cggw).get(1);
cggw.CGGW_WSM_AHU_Count__c = cggwCounts.get(cggw).get(2);
upsert(cggw);
}
}
In your case, you are using the Control_Gateway__c object cggw to look up values in your map. However, in your for loop, you are also changing fields on cggw. As soon as cggw changes, it no longer matches the Control_Gateway__c object that is the key to the map, hence the NullPointerException.
All Answers
I doubt this usage of map is causing this error cggwCounts.get(cggw).get(0) .
To ensure this try adding an IF condition in side for loop as follows
for (Control_Gateway__c cggw: cggwCounts.keySet()) {
If(cggwCounts.get(cggw)!=null){ // NEW LINE
cggw.CGGW_WAM_Count__c = cggwCounts.get(cggw).get(0);
cggw.CGGW_WSM_Power_Count__c = cggwCounts.get(cggw).get(1);
cggw.CGGW_WSM_AHU_Count__c = cggwCounts.get(cggw).get(2);
upsert(cggw);
}
}
Let me know the outcome.
Cheers..!
Thanks for the response. I have included your if statement (as well as a few others) but am still getting the same error. Here is the pertinent change (with added line numbers) along with the null-reference error I get in the debug logs.
(51) for (Control_Gateway__c cggw: cggwCounts.keySet()) {
(52) if(cggwCounts.get(cggw)!=null){
(53) if (cggwCounts.get(cggw).get(0) != null){ if (cggw.CGGW_WAM_Count__c != null){
(54) cggw.CGGW_WAM_Count__c = cggwCounts.get(cggw).get(0);}}
(55) if (cggwCounts.get(cggw).get(1) != null){ if (cggw.CGGW_WSM_Power_Count__c != null){
(56) cggw.CGGW_WSM_Power_Count__c = cggwCounts.get(cggw).get(1);}}
(57) if (cggwCounts.get(cggw).get(2) != null){ if (cggw.CGGW_WSM_AHU_Count__c != null){
(58) cggw.CGGW_WSM_AHU_Count__c = cggwCounts.get(cggw).get(2);}}
(59) upsert(cggw);
(60) }
(61) }
(62) }
12:23:22.182 (182026812)|SYSTEM_CONSTRUCTOR_ENTRY|[51]|<init>()
12:23:22.182 (182047308)|SYSTEM_CONSTRUCTOR_EXIT|[51]|<init>()
12:23:22.182 (182063132)|SYSTEM_METHOD_ENTRY|[51]|MAP<Control_Gateway__c,LIST<Integer>>.keySet()
12:23:22.182 (182155903)|SYSTEM_METHOD_EXIT|[51]|MAP<Control_Gateway__c,LIST<Integer>>.keySet()
12:23:22.182 (182181079)|SYSTEM_METHOD_ENTRY|[51]|LIST<Control_Gateway__c>.addAll(Object)
12:23:22.182 (182202836)|SYSTEM_METHOD_EXIT|[51]|LIST<Control_Gateway__c>.addAll(Object)
12:23:22.182 (182211781)|SYSTEM_METHOD_ENTRY|[51]|LIST<Control_Gateway__c>.iterator()
12:23:22.182 (182423710)|SYSTEM_METHOD_EXIT|[51]|LIST<Control_Gateway__c>.iterator()
12:23:22.182 (182457645)|SYSTEM_METHOD_ENTRY|[51]|system.ListIterator.hasNext()
12:23:22.182 (182472809)|SYSTEM_METHOD_EXIT|[51]|system.ListIterator.hasNext()
12:23:22.182 (182502241)|SYSTEM_METHOD_ENTRY|[52]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182533635)|SYSTEM_METHOD_EXIT|[52]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182552509)|SYSTEM_METHOD_ENTRY|[53]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182572756)|SYSTEM_METHOD_EXIT|[53]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182584185)|SYSTEM_METHOD_ENTRY|[53]|LIST<Integer>.get(Integer)
12:23:22.182 (182594952)|SYSTEM_METHOD_EXIT|[53]|LIST<Integer>.get(Integer)
12:23:22.182 (182652449)|SYSTEM_METHOD_ENTRY|[54]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182676869)|SYSTEM_METHOD_EXIT|[54]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182691817)|SYSTEM_METHOD_ENTRY|[54]|LIST<Integer>.get(Integer)
12:23:22.182 (182701689)|SYSTEM_METHOD_EXIT|[54]|LIST<Integer>.get(Integer)
12:23:22.182 (182776661)|SYSTEM_METHOD_ENTRY|[55]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182801649)|SYSTEM_METHOD_EXIT|[55]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182813312)|SYSTEM_METHOD_ENTRY|[55]|LIST<Integer>.get(Integer)
12:23:22.182 (182822833)|SYSTEM_METHOD_EXIT|[55]|LIST<Integer>.get(Integer)
12:23:22.182 (182867031)|SYSTEM_METHOD_ENTRY|[56]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182890266)|SYSTEM_METHOD_EXIT|[56]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182902039)|SYSTEM_METHOD_ENTRY|[56]|LIST<Integer>.get(Integer)
12:23:22.182 (182911369)|SYSTEM_METHOD_EXIT|[56]|LIST<Integer>.get(Integer)
12:23:22.182 (182968562)|SYSTEM_METHOD_ENTRY|[57]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.182 (182991052)|SYSTEM_METHOD_EXIT|[57]|MAP<Control_Gateway__c,LIST<Integer>>.get(Object)
12:23:22.183 (183181964)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
Trigger.updateGatewayCounts: line 57, column 1
12:23:22.183 (183197699)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
If is it so then I think you should debug cggwCounts before that For loop and see whether there is a third value in it. so only if there is are 3 items in it you can use cggwCounts.get(cggw).get(2);.
check it and let me know...
In your case, you are using the Control_Gateway__c object cggw to look up values in your map. However, in your for loop, you are also changing fields on cggw. As soon as cggw changes, it no longer matches the Control_Gateway__c object that is the key to the map, hence the NullPointerException.