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
mikefmikef 

System.NullPointerException: Attempt to de-reference a null object

I am getting this error and I don't know what is causing it.
I am creating a Map in my Trigger that has a String and SObject values.
I think the issue is I am not setting the Country in my Map, but I am not sure how to do this.

What am I doing wrong.
Thanks for you time.

Code:
trigger hooverAddressFileds on Account bulk (before update) {

//create a string set of all the countries that are in the update file 
 Set<String> country = new Set<String>();
 for(Account accounts : Trigger.new){
  country.add(accounts.Hoover_s_Country__c);
 }

//create a Map of all the Set
 Map<String, iso_country_code__c> iso = new Map<String, iso_country_code__c>([select ISO_code__c from iso_country_code__c where Country__c in :country]);
//updating the ISO code on the billing and shipping country
 for(Account accounts : Trigger.new){
  if(accounts.Hoover_s_Country__c != null){
   accounts.billingcountry = iso.get(accounts.Hoover_s_Country__c).ISO_code__c;
   accounts.shippingcountry = iso.get(accounts.Hoover_s_Country__c).ISO_code__c; 
  }
 }
//updatting billing and shipping street with the new Hoovers address field
 for(Account accounts : Trigger.new){
  if(accounts.Hoover_s_Address_Two__c != null && accounts.Hoover_s_Address_One__c != null){
   accounts.BillingStreet = accounts.Hoover_s_Address_One__c + ' ' + accounts.Hoover_s_Address_Two__c;
   accounts.ShippingStreet = accounts.Hoover_s_Address_One__c + ' ' + accounts.Hoover_s_Address_Two__c; 
  }
 }
//nulling out the hoovers fields
 for(Account accounts : Trigger.new){
  accounts.Hoover_s_Address_One__c = null;
  accounts.Hoover_s_Address_Two__c = null;
  accounts.Hoover_s_Country__c = null;
 }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber
I think the problem is that the map is keyed by the Id and not the country:

Map<String, iso_country_code__c> iso = new Map<String, iso_country_code__c>([select ISO_code__c from iso_country_code__c where Country__c in :country]);

The automatic map generation using the SOQL statement pattern per above always uses the Id as the key. So when you make this statement:

accounts.billingcountry = iso.get(accounts.Hoover_s_Country__c).ISO_code__c;

You are asking the iso map for an Iso_country_code__c object by a key that cannot be in your map (unless you have some strange country values) ;-). Thus your value will be null and you'll get this exception because you are trying to get a field value off a null object. If that's what you are expecting then you don't need to generate a map using the Id as the key. Instead you'll want to iterate through the results of your query and put the iso_country_code__c into the map, keyed by the country__c (at least that's what it appears you want from what I can tell):

Map<String, iso_country_code__c> countryIsoMap = new Map<String, iso_country_code__c>();

for(iso_country_code__c iso_code:[select iso_code__c, country__c from iso_country_code__c where country__c in :country]) { 
   if(iso_code.country__c != null) countryIsoMap.put(iso_code.country__c, iso_code);
}

Now, assuming your map isn't empty for whatever reason, you should have what you need.... if I understand correctly.

All Answers

mtbclimbermtbclimber
I think the problem is that the map is keyed by the Id and not the country:

Map<String, iso_country_code__c> iso = new Map<String, iso_country_code__c>([select ISO_code__c from iso_country_code__c where Country__c in :country]);

The automatic map generation using the SOQL statement pattern per above always uses the Id as the key. So when you make this statement:

accounts.billingcountry = iso.get(accounts.Hoover_s_Country__c).ISO_code__c;

You are asking the iso map for an Iso_country_code__c object by a key that cannot be in your map (unless you have some strange country values) ;-). Thus your value will be null and you'll get this exception because you are trying to get a field value off a null object. If that's what you are expecting then you don't need to generate a map using the Id as the key. Instead you'll want to iterate through the results of your query and put the iso_country_code__c into the map, keyed by the country__c (at least that's what it appears you want from what I can tell):

Map<String, iso_country_code__c> countryIsoMap = new Map<String, iso_country_code__c>();

for(iso_country_code__c iso_code:[select iso_code__c, country__c from iso_country_code__c where country__c in :country]) { 
   if(iso_code.country__c != null) countryIsoMap.put(iso_code.country__c, iso_code);
}

Now, assuming your map isn't empty for whatever reason, you should have what you need.... if I understand correctly.
This was selected as the best answer