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
sumanth v 18sumanth v 18 

Null pointer exception on converstion rates

I am trying to fetch converstion rate in Billingmonth [Custom Object] batch job
[Converstion rate- Currency Exchange]
      Map<String, DatedConversionRate> conversionRates = new Map<String, DatedConversionRate>();
      set < string > ISOCodes = new set < string > ();    
      String dtStr;
      Date dt;    
for (sObject Oppline: scope) {
    opportunitylineitem op = (opportunitylineitem) Oppline;  
    Lineid.add(op.LineId__c);
    ISOCodes.add(op.CurrencyIsoCode);
    } 
dtStr =system.today().year()+ '-'+ system.today().month()+ '-'+'01';
       dt = Date.valueof(dtStr);
              
       if (ISOCodes.size() > 0) {
        for (DatedConversionRate ex: [Select id, StartDate, ConversionRate, IsoCode from DatedConversionRate where StartDate >= : dt and IsoCode IN: ISOCodes]) {
       system.debug('@@@DateKey-->' + string.ValueOf(ex.StartDate.month()) + string.ValueOf(ex.StartDate.year()));
       conversionRates.put(string.ValueOf(ex.StartDate.month()) + string.ValueOf(ex.StartDate.year()), ex);
            system.debug('@@@@@@@@'+conversionRates+bm.Placement_Line_ID__c);
            }
       }
       String str;
       str = String.ValueOf(system.today().month()) + String.ValueOf(system.today().year());
       system.debug('@@@@@@@@123'+conversionRates);
       
       bm.Conversion_Rate__c = conversionRates.get(str).ConversionRate; [I am getting null pointer exception in this line.]
 
v varaprasadv varaprasad
Hi Sumanth,

Here you are assigning null value =check this one in debug log values( conversionRates.get(str).ConversionRate);

Thanks
Varaprasad
Waqar Hussain SFWaqar Hussain SF
try below code snippet.
 
if(conversionRates.get(str) != null)
bm.Conversion_Rate__c = conversionRates.get(str).ConversionRate;