You need to sign in to do that
Don't have an account?
FJDEV
custom setting Attempt to de-reference a null object
Here is my code
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://api.geonames.org/countryCode?lat=52.42452&lng=10.7815&username=***');
req.setHeader('Content-Type', 'text/html');
req.setMethod('GET');
HttpResponse res = h.send(req);
System.debug('country' + res.getBody()); ( return 'DE' as result)
String Country = (String)res.getBody();
System.debug('the return value is'+Country); ( 'DE' as result )
ISOContry__c cs = ISOContry__c.getInstance(Country);
System.debug(cs); ( return null as result)
System.debug (' the A3 '+ cs.A3__c); ( Attempt to de-reference a null object)
But when I try just to give the value as String it is working so that's mean that the custom setting has the name 'DE' ! so really I don't understand what could be the problem
ISOContry__c cs2= ISOContry__c.getInstance('DE');
System.debug(cs2.A3__c); (it is working as expected)
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://api.geonames.org/countryCode?lat=52.42452&lng=10.7815&username=***');
req.setHeader('Content-Type', 'text/html');
req.setMethod('GET');
HttpResponse res = h.send(req);
System.debug('country' + res.getBody()); ( return 'DE' as result)
String Country = (String)res.getBody();
System.debug('the return value is'+Country); ( 'DE' as result )
ISOContry__c cs = ISOContry__c.getInstance(Country);
System.debug(cs); ( return null as result)
System.debug (' the A3 '+ cs.A3__c); ( Attempt to de-reference a null object)
But when I try just to give the value as String it is working so that's mean that the custom setting has the name 'DE' ! so really I don't understand what could be the problem
ISOContry__c cs2= ISOContry__c.getInstance('DE');
System.debug(cs2.A3__c); (it is working as expected)
for (ISOContry__c item : ISOContry__c.getAll().values()) {
if(item.Name==country){land =item.A3__c;
System.debug('land'+ land);
}
}
//Try by replacing this Line
String Country = (String)res.getBody();
//With this line
String Country = (String)JSON.deserializeUntyped(res.getBody());
Can you try this way
ISOContry__c cs = ISOContry__c.getValues(string.valueOf(Country));
if(cs <> NULL) {
//do your logic
System.debug (' the A3 '+ cs.A3__c);
}
And Check if your custom setting Name had same Country (DE) value.
if your custom setting doesn;t have requested value it will be null so we can't access null.A3__c
I will recoment to use the custom metadata type rather than custom settings
yes the value is there ! I think that the problem is with the response because I tried to insert the String which has been returned from the response but without success !! here the endpoint: http://api.geonames.org/countryCode?lat=52.42452&lng=10.7815&username=ilumefiras
it will returned DE as result . Do you have an idea ?