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
FJDEVFJDEV 

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)

 
FJDEVFJDEV
thank you for your response ! but I didn't get what you mean exactly . Could you please  tell me which line of my code is wrong and  how can I fix it .
FJDEVFJDEV
I tried also with this loop but it doesn't return any values
for (ISOContry__c item : ISOContry__c.getAll().values()) {
     
           if(item.Name==country){land =item.A3__c;
                    System.debug('land'+ land);
                            }
     }
Sonali_takkeSonali_takke
Hi,
//Try by replacing  this Line  
String Country = (String)res.getBody();   

//With this line
String Country = (String)JSON.deserializeUntyped(res.getBody());
FJDEVFJDEV
I just get this error : System.JSONException: Unexpected character ('D' (code 68)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]
Raj VakatiRaj Vakati
Are you using list or Hierarchy  custom settings ?? Change code as below 
 
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.getValues(Country);
System.debug(cs); ( return null as result)
System.debug (' the A3 '+ cs.A3__c); ( Attempt to de-reference a null object)

 
FJDEVFJDEV
I am using list cutom setting
Raj VakatiRaj Vakati
try this code
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 )

<b>ISOContry__c cs = ISOContry__c.getValues(Country);
</b>System.debug(cs); ( return null as result)
System.debug (' the A3 '+ cs.A3__c); ( Attempt to de-reference a null object)

 
FJDEVFJDEV
The same error ! i tried also with custom object and doing soql Query but it doesn't work !
Balu_SFDCBalu_SFDC
HI,

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
 
Raj VakatiRaj Vakati
Do you have the values in the custom setting ??check once pls ..

I will recoment to use the custom metadata type rather than custom settings 
FJDEVFJDEV
Thank you .
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 ?
Raj VakatiRaj Vakati
let try this
 
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);

if(res.getStatusCode()==200){
 System.debug('country' + res.getBody()); ( return 'DE' as result)
 String Country = (String)res.getBody();
System.debug('the return value is'+Country); ( 'DE' as result )
If(Country!=null){
<b>ISOContry__c cs = ISOContry__c.getValues(Country);
If(cs!=null){
</b>System.debug(cs); ( return null as result)
System.debug (' the A3 '+ cs.A3__c); ( Attempt to de-reference a null object)
}
}
}

 
Sonali_takkeSonali_takke
Is it solved ?