+ Start a Discussion
NeelaNeela 

Hii!Can someone help me with this REST API callout which is getting error like "System.NullPointerException: Attempt to de-reference a null object"

CLASS:
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('https://api.exchangeratesapi.io/latest');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
    
}
}

Testing at anonymous window:
decimal conversionrate = Currencyconverter.CurrencyconverterfromEUROtoINR(150);
system.debug('conversionrate'+conversionrate);

ERROR:
System.NullPointerException: Attempt to de-reference a null object
Best Answer chosen by Neela
Maharajan CMaharajan C
Hi Neela,

You are missing the missing the API access_key in endpoit url it's mandatory to get the exchange rate.

Please follow the below steps:

1. Change the Endpoint URL : ( The below access key is dummy)
http://api.exchangeratesapi.io/v1/latest?access_key=23324237856784748&format=1

2. Create your free access key in https://exchangeratesapi.io/
https://exchangeratesapi.io/pricing/


3. Updatet the remote site end point  in your salesforce org as below (setup -> Remote Site Settings):
https://api.exchangeratesapi.io

4. Please use the below exchangeratesapi documentation also.
https://exchangeratesapi.io/documentation/
https://manage.exchangeratesapi.io/quickstart
  --- (login the  exchangeratesapi  in website)
 
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('http://api.exchangeratesapi.io/v1/latest?access_key=23324237856784748&format=1');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
        
    }
}

Thanks,
Maharajan.C

All Answers

Danish HodaDanish Hoda
Hi Neela,
Debug the error by following below steps -
* use debug log to check the data in res.getBody()
* see if the Jsonbody contains 'rates' as one of the keys
* see if the inner JSON fr rates, if available, has 'INR' as one of the keys.
Maharajan CMaharajan C
Hi Neela,

You are missing the missing the API access_key in endpoit url it's mandatory to get the exchange rate.

Please follow the below steps:

1. Change the Endpoint URL : ( The below access key is dummy)
http://api.exchangeratesapi.io/v1/latest?access_key=23324237856784748&format=1

2. Create your free access key in https://exchangeratesapi.io/
https://exchangeratesapi.io/pricing/


3. Updatet the remote site end point  in your salesforce org as below (setup -> Remote Site Settings):
https://api.exchangeratesapi.io

4. Please use the below exchangeratesapi documentation also.
https://exchangeratesapi.io/documentation/
https://manage.exchangeratesapi.io/quickstart
  --- (login the  exchangeratesapi  in website)
 
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('http://api.exchangeratesapi.io/v1/latest?access_key=23324237856784748&format=1');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
NeelaNeela
Hii Maharajan.C,
I'm getting the same error "System.NullPointerException: Attempt to de-reference a null object"  after updating endpoint url and API access_key while testing in anonymous window.pls help me with this.
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('http://api.exchangeratesapi.io/v1/YYYY-MM-DD');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
        
    }
}

-------------and in anonymous window--------------------
decimal conversionrate = Currencyconverter.CurrencyconverterfromEUROtoINR(150);
system.debug('conversionrate'+conversionrate);


Thanks for the information about the accsess key.
 
Maharajan CMaharajan C
Hi Neela,

I have used my access code in below:

Just try the below code without change anything:
 
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('http://api.exchangeratesapi.io/v1/latest?access_key=56f8c53669c648dc2916360bff5cceed&format=1');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
        
    }
}

Thanks,
Maharajan.C