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
Vigneshwaran LoganathanVigneshwaran Loganathan 

Attempt to de-reference a null object from Rest API class.

Hi Guys, 

I have written a simple REST class to connect two SF orgs (lets say A and B), 

in A, i have created remote site '' and wrote below class, 
global with sharing class RESTCreateNewAccount{

    global static string createAccountRecord(String name) {
     string succError;
     account acc = new account(); = name;       
     insert acc;
     acc.accountsource = 'Other';
     update acc;
     succError = 'New account has been created. Account Record id is:';
     return succError;
    global static string updateAccountRecord(String name) {
       string returnmessage = 'Account are updated successfully';
       List<account> accupdateList = [select name,id,AccountSource from account where name =: name];
       for(account acc : accupdateList){
           acc.AccountSource = 'Web' ;
           returnmessage = returnmessage;
         update accupdateList;
       catch(exception ex){
         returnmessage = ex.getmessage();
       return returnmessage ;

in Org B, i have written Below class,
public class RestAPIcalloutExample{

   Public void loginToOtherOrgAndCreaetNewAccount(){
        /*Login to Other Salesforce Org to grab session id - begin*/
        HTTP h1 = new HTTP();
        HttpRequest request = new HttpRequest();       
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns=""><Header/><Body><login xmlns=""><username>' + ''+ '</username><password>' +'makil1234'+ '</password></login></Body></Envelope>');
        String SERVER_URL;
        String SESSION_ID;
        HTTPResponse loginResponse = h1.send(request);     
        Dom.Document doc = loginResponse.getBodyDocument();
        Dom.XMLNode receivedXml= doc.getRootElement();
        SESSION_ID = receivedXml.getChildElement('Body', '').getChildElement('loginResponse', '').getChildElement('result','').getChildElement('sessionId','') .getText();      
        system.debug('***session id'+SESSION_ID); 
        /*Login to Other salesforce Org to grab session id - finish*/
        /* Send https request to the custom REST API defined in other org that has urlmapping as 'createNewAccountUsingRestAPI' -begin*/
        HTTP h2 = new HTTP();
        HttpRequest request2CreateAccount = new HttpRequest();
        request2CreateAccount.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request2CreateAccount.setHeader('Accept', 'application/json');
        request2CreateAccount.setHeader('SOAPAction', '""');
        request2CreateAccount.setbody('{"name":"Cloudforce4u Technologies Ltd"}');
        request2CreateAccount.setHeader('Authorization', 'OAuth '+SESSION_ID);             
        HTTPResponse resppnceFromOtherSFOrg = h2.send(request2CreateAccount);
        /* Send https request to the custom REST API defined in other org that hass urlmapping as updateaccountrec - finish*/

i tried to run this from Dev Console from Org B, its showing 'de-reference a null object' Error.. What is the mistake here.? 

SandhyaSandhya (Salesforce Developers) 

I have not tried your code, but I would suggest you enable debug mode and see which line you are getting this error.

For your reference below link explains all the steps for salesforce integration using REST API.
Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Eswar Force.comEswar
Hi Vignesh,

Error says that memory allocation is not their in object,But i saw your code it seems to be good.Recently i posted same requirement if you see means i hope your problem get resolved.

If this helps you please mark it as solved.

Thanks and Regards,
Eswar Prasad