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
Mohan Chaitanya PalaparthiMohan Chaitanya Palaparthi 

There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object

I'm trying to complete Apex Integration Services rest services i  wrote code everything was good but when check challenge i got the error which i have mentioned above
Best Answer chosen by Mohan Chaitanya Palaparthi
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

Please try below piece of code which works fine for me.

Apex Class:
public class AnimalLocator {
	public class cls_animal {
		public Integer id;	
		public String name;	
		public String eats;	
		public String says;	
	}    
public class JSONOutput{
	public cls_animal animal;

   	//public JSONOutput parse(String json){
	//return (JSONOutput) System.JSON.deserialize(json, JSONOutput.class);
	//}
}
    
    public static String getAnimalNameById (Integer id) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
        //request.setHeader('id', String.valueof(id)); -- cannot be used in this challenge :)
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug('response: ' + response.getBody());
        //Map<String,Object> map_results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
        jsonOutput results = (jsonOutput) JSON.deserialize(response.getBody(), jsonOutput.class);
        //Object results = (Object) map_results.get('animal');
		system.debug('results= ' + results.animal.name);
        return(results.animal.name);
    }

}
Animal Locator Mock:
@IsTest
global class AnimalLocatorMock implements HttpCalloutMock {
    
    global HTTPresponse respond(HTTPrequest request) {
        Httpresponse response = new Httpresponse();
        response.setStatusCode(200);
        //-- directly output the JSON, instead of creating a logic
        //response.setHeader('key, value)
        //Integer id = Integer.valueof(request.getHeader('id'));
        //Integer id = 1;
        //List<String> lst_body = new List<String> {'majestic badger', 'fluffy bunny'};
        //system.debug('animal return value: ' + lst_body[id]);
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        return response;
    }

}
Animal Locator Test:
@IsTest
public class AnimalLocatorTest {
    @isTest
    public static void testAnimalLocator() {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        //Httpresponse response = AnimalLocator.getAnimalNameById(1);
        String s =  AnimalLocator.getAnimalNameById(1);
        system.debug('string returned: ' + s);
    }

}
Please let us know if this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Make sure that you are using new org as this may due to pre existing configuration that you might have done for other challenge.

Best Regards,
Sandhya
Mohan Chaitanya PalaparthiMohan Chaitanya Palaparthi
Hi sandhya
            I have done this in a new trailhead playground and i tried that for twice still it is showing the same error
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

sorry for this issue you are facing.

May I request you please post the code snippet of what you have done so far so that we can look into it and can help you accordingly.

Thanks,
Nagendra
Mohan Chaitanya PalaparthiMohan Chaitanya Palaparthi
my apex class 

public class AnimalLocator {
    
      public static string  getAnimalNameById(integer id) {
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
          
          String stg = '';
          system.debug('---response----'+response.getBody());
          system.debug('---response----'+response.getStatusCode());
          
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
             // Cast the values in the 'animals' key as a Map
            Map<String, Object> animals = (MAp<String, Object>) results.get('animals');
  
                         System.debug('Received the following animals:'+animals); 
                         stg = string.valueof(animals.get('name'));
                         System.debug('---stg---'+stg); 
            }
        return stg;
    }

mock
@istest
global class AnimalLocatorMock implements HttpCalloutMock {
    
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody ('{"animals": {("id": 1,"name":"cow")}}');
        response.setStatusCode(200);
        return response; 
    }

}

test class
@isTest
private class AnimalLocatorTest{

       @isTest static  void AnimalLocatorMock1() {
        Test.SetMock(HttpCallOutMock.class, new AnimalLocatorMock());
        string result=AnimalLocator.getAnimalNameById(3);
        string expectedResult='cow';
        System.assertEquals(result, expectedResult);
    }
}
Mohan Chaitanya PalaparthiMohan Chaitanya Palaparthi
hi nagendra
      can you please help me out
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

Please try below piece of code which works fine for me.

Apex Class:
public class AnimalLocator {
	public class cls_animal {
		public Integer id;	
		public String name;	
		public String eats;	
		public String says;	
	}    
public class JSONOutput{
	public cls_animal animal;

   	//public JSONOutput parse(String json){
	//return (JSONOutput) System.JSON.deserialize(json, JSONOutput.class);
	//}
}
    
    public static String getAnimalNameById (Integer id) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
        //request.setHeader('id', String.valueof(id)); -- cannot be used in this challenge :)
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug('response: ' + response.getBody());
        //Map<String,Object> map_results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
        jsonOutput results = (jsonOutput) JSON.deserialize(response.getBody(), jsonOutput.class);
        //Object results = (Object) map_results.get('animal');
		system.debug('results= ' + results.animal.name);
        return(results.animal.name);
    }

}
Animal Locator Mock:
@IsTest
global class AnimalLocatorMock implements HttpCalloutMock {
    
    global HTTPresponse respond(HTTPrequest request) {
        Httpresponse response = new Httpresponse();
        response.setStatusCode(200);
        //-- directly output the JSON, instead of creating a logic
        //response.setHeader('key, value)
        //Integer id = Integer.valueof(request.getHeader('id'));
        //Integer id = 1;
        //List<String> lst_body = new List<String> {'majestic badger', 'fluffy bunny'};
        //system.debug('animal return value: ' + lst_body[id]);
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        return response;
    }

}
Animal Locator Test:
@IsTest
public class AnimalLocatorTest {
    @isTest
    public static void testAnimalLocator() {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        //Httpresponse response = AnimalLocator.getAnimalNameById(1);
        String s =  AnimalLocator.getAnimalNameById(1);
        system.debug('string returned: ' + s);
    }

}
Please let us know if this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 
This was selected as the best answer
meher deepak pappurmeher deepak pappur
I have been facing issue for more than 1 hour and i finally saw Nagendra Sir post, thank you so much for the help, it finally worked and i am happy