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 'Login.salesforce.com' and wrote below class, 
 
@RestResource(urlMapping='/createNewAccountUsingRestAPI/*')
global with sharing class RESTCreateNewAccount{

    @HttpPost
    global static string createAccountRecord(String name) {
     string succError;
     account acc = new account();
     acc.name = name;       
     insert acc;
     acc.accountsource = 'Other';
     update acc;
     succError = 'New account has been created. Account Record id is:'+acc.id;
     return succError;
    }
  
    @httpput
    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 +acc.id;
       }
       try{
         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.setEndpoint('https://login.salesforce.com/services/Soap/u/22.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setTimeout(60000);
        request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' + 'vigneshwaranl@hotmail.com'+ '</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', 'http://schemas.xmlsoap.org/soap/envelope/').getChildElement('loginResponse', 'urn:partner.soap.sforce.com').getChildElement('result','urn:partner.soap.sforce.com').getChildElement('sessionId','urn:partner.soap.sforce.com') .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.setMethod('POST');
        request2CreateAccount.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request2CreateAccount.setHeader('Accept', 'application/json');
        request2CreateAccount.setHeader('SOAPAction', '""');
        request2CreateAccount.setbody('{"name":"Cloudforce4u Technologies Ltd"}');
            request2CreateAccount.setEndpoint('https://ap1.salesforce.com/services/apexrest/createNewAccountUsingRestAPI');
        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*/
        system.debug('**-Output-**'+resppnceFromOtherSFOrg.getbody());
   } 
 }

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

Thanks, 
Vignesh
SandhyaSandhya (Salesforce Developers) 
Hi,

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.

https://jabeecloud.wordpress.com/2015/10/04/salesforce-integration-across-two-different-organizations-using-rest-api-and-rest-web-service-and-apex-web-service/
 
Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya
Eswar Force.comEswar Force.com
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.

http://eswarforce.blogspot.in/

If this helps you please mark it as solved.

Thanks and Regards,
Eswar Prasad