function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Masechaba Maseli 8Masechaba Maseli 8 

attemt to de-reference a null object

Hi all
I am not sure why I am getting this error on my trigger, may someone please point me in the right direction. 
 
trigger PopulateRegion on Shipment_Order__c (after insert, after update) {

for(Shipment_Order__c shipmentorder : Trigger.new) {

If(shipmentorder.Hub_Shipment_Formula__c == False ){

        string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
        shipmentorder.Ship_From_Zone1__c = region; 
}
    
    Else { 
    string region3 = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
    string region2 = Country_Region_List__c.getInstance(shipmentorder.HUB_Country_Formula__c).Region__c;
    shipmentorder.Ship_From_Zone1__c = region3;
    shipmentorder.Hub_Ship_From_Zone__c= region2;

}

}
}

 
Best Answer chosen by Masechaba Maseli 8
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi Masechaba Maseli 8,

As i observed you have not creating custom setting record with the value of HUB_Country_Formula__c field. Try to add custom setting record and try.

As Abdul Khatri mentioned, dont forget to have a null check.

Let me know if you need more help.

Regards
Naveen
 

All Answers

Abdul KhatriAbdul Khatri
You didn't provide the detail about the errors I mean't the line number etc. but normally it happens when the value of the object or field is null. I can only guess as I don't have the details, please check if this line is giving you an issue whcih mean Hub_Shipment_Formula__c has a null value

User-added image
Masechaba Maseli 8Masechaba Maseli 8
Hi Abdul

The issue is on line 13, the Hub_Shipment__c is a checkbox.
Abdul KhatriAbdul Khatri
I would recommend to handle the null values as much as possible. Please try to do this and check if this works
 
if(Country_Region_List__c.getInstance(shipmentorder.HUB_Country_Formula__c) != null {
string region2 = Country_Region_List__c.getInstance(shipmentorder.HUB_Country_Formula__c).Region__c;
}

 
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi Masechaba Maseli 8,

As i observed you have not creating custom setting record with the value of HUB_Country_Formula__c field. Try to add custom setting record and try.

As Abdul Khatri mentioned, dont forget to have a null check.

Let me know if you need more help.

Regards
Naveen
 
This was selected as the best answer
Abdul KhatriAbdul Khatri
@Naveen This is what my intent was as I think there is no value available for assignment.
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi Masechaba Maseli,

Did you added the value in custom setting and cross checked?

If you did and that helped you to fix the issue, then mark respectrive answer as Best answer.
Masechaba Maseli 8Masechaba Maseli 8
Hi Naveen

It was spelled differently. Thank you for the help.