0

I am getting attempt to de-reference null object error the below callouts.

Class 1:

public class ProcessImgCallout {

    @future (callout=true)

    public static void SendImage(ID id) {
        system.debug('hello Mahesh '+id);

       String endPointURL = 'https://cloud.ocrsdk.com/processBusinessCard?exportformat=xml';
      String userName = '';// i removed username for security puropse
      String password = '';//i removed password for security puropse
      ContentVersion co = [Select id,pathOnClient,title,versionData  from ContentVersion where id =:id];
      Blob img =co.versionData;

        Blob headerValue = Blob.valueOf(userName + ':' + password);
         String authorizationHeader = 'BASIC ' +
           EncodingUtil.base64Encode(headerValue);
           Httprequest request = new HttpRequest();
             Http http = new Http();

             request.setMethod('POST');
              request.setEndpoint(endPointURL);
              request.setHeader('Authorization', authorizationHeader);
              request.setHeader('Content-Type', 'image/jpeg');
               request.setTimeout(2 * 60 * 1000);

               request.setBodyAsBlob(img);          

             HttpResponse res = http.send(request);  

              System.debug('responseBody: '+res.getBody());

         Dom.Document docx = new Dom.Document();
             //docx.load(responseUrl2);
             system.debug('@@@'+res.getbody());
             docx.load(res.getbody()); 
             //system.debug('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ '+doc);
           String taskId = docx.getRootElement().getChildElement('task', null).getAttribute('id', null);
           system.debug(taskId);
        gettaskStatus.GetStatus(taskId);
    }
}

Class 2:

public class gettaskStatus {

    public static void GetStatus(String id){
        system.debug('hello mahesh i am with task id to get status'+id);
        //String taskid=id;


         String endPointURL = 'http://cloud.ocrsdk.com/getTaskStatus?taskId='+id;
        system.debug(endPointURL);
         String userName = '';//i removed username for security puropse
          String password = '';//i removed password for security puropse
          Blob headerValue = Blob.valueOf(userName + ':' + password);
         String authorizationHeader = 'BASIC ' +
           EncodingUtil.base64Encode(headerValue);
           Httprequest req = new HttpRequest();
             Http ht = new Http();

              req.setMethod('GET');
              req.setEndpoint(endPointURL);
              req.setHeader('Authorization', authorizationHeader);
              req.setTimeout(2 * 60 * 1000);
              //req.setHeader('Content-Type', 'image/jpeg');



             HttpResponse resp = ht.send(req);  

              System.debug('responseBody: to download url '+resp.getBody());

             Dom.Document doc = new Dom.Document();
             //docx.load(responseUrl2);
             system.debug('@@@'+resp.getbody());
             doc.load(resp.getbody()); 
             //system.debug('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ '+doc);
           String resulturl = doc.getRootElement().getChildElement('task', null).getAttribute('resultUrl', null);
           system.debug(resulturl);
        GetLeadfromUrl.GetDownloadUrl(resulturl);

    }

}

Class 3:

public class GetLeadfromUrl {

     public static void GetDownloadUrl(String url){
         String ROOT_TAG = 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd';
       // system.debug(url);
        String downloadurl=url;
         String newUrl=downloadurl.remove('amp;');
          system.debug(newUrl);

         String endPointURL =newUrl;

         Httprequest reqq = new HttpRequest();
             Http htt = new Http();

              reqq.setMethod('GET');
              reqq.setEndpoint(endPointURL);
              //reqq.setHeader('Authorization', authorizationHeader);
              //req.setHeader('Content-Type', 'image/jpeg');



             HttpResponse respp = htt.send(reqq);  

              System.debug('responseBody: to get final response '+respp.getBody());
                 Dom.Document doc = new Dom.Document();
             //d.load(responseUrl2);
             doc.load(respp.getbody());
             system.debug('@@@'+respp.getbody());
           Dom.XMLNode root = doc.getRootElement();
Dom.XMLNode businessCard = root.getChildElement('businessCard', 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd');
Lead l = new Lead();

for (Dom.XmlNode node : businessCard.getChildren()) {
    if (node.getAttribute('type', null) != null) {
        if (node.getAttribute('type', null) == 'Phone') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                l.Phone = node.getChildElement('value', ROOT_TAG).getText();
            }
        }
        if (node.getAttribute('type', null) == 'Email') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                l.Email = node.getChildElement('value', ROOT_TAG).getText();
            }
        }
        if (node.getAttribute('type', null) == 'Company') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                l.Company = node.getChildElement('value', ROOT_TAG).getText();
            }
        }
        if (node.getAttribute('type', null) == 'Name') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                String name = node.getChildElement('value', ROOT_TAG).getText();
                String[] splited = name.split('\\s+');
                if(splited[0] != null || splited[0] != '')
                    l.FirstName = splited[0];

                if(splited[1] != null || splited[1] != '') 
                    l.LastName = splited[1];
            }
        }


    }
}

insert l;



    }  

}

Logs:

Class.GetLeadfromUrl.GetDownloadUrl: line 7, column 1
Class.gettaskStatus.GetStatus: line 37, column 1
Class.ProcessImgCallout.SendImage: line 39, column 1
16:10:36.29 (526284477)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.GetLeadfromUrl.GetDownloadUrl: line 7, column 1
Class.gettaskStatus.GetStatus: line 37, column 1
Class.ProcessImgCallout.SendImage: line 39, column 1
16:10:36.526 (526290597)|CUMULATIVE_LIMIT_USAGE
16:10:36.526 (526290597)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 200
Number of query rows: 1 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Maximum CPU time: 0 out of 60000
Maximum heap size: 0 out of 12000000
Number of callouts: 2 out of 100
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 1
Number of Mobile Apex push calls: 0 out of 10

16:10:36.526 (526290597)|CUMULATIVE_LIMIT_USAGE_END

16:10:36.29 (526366927)|CODE_UNIT_FINISHED|ProcessImgCallout.SendImage
16:10:36.29 (528606138)|EXECUTION_FINISHED

I am getting attempt to de-reference null object error in the second class. How can I fix it?

The actual response should be:

<?xml version="1.0" encoding="utf-8"?>
<response>
    <task id="5db7594c-721d-4ec4-b922-c07b03d24a76" registrationTime="2017-06-12T11:28:30Z" statusChangeTime="2017-06-12T11:28:30Z" status="Completed" filesCount="1" credits="0" resultUrl="https://ocrsdk.blob.core.windows.net/files/5db7594c-721d-4ec4-b922-c07b03d24a76.result?sv=2012-02-12&amp;se=2017-06-12T23%3A00%3A00Z&amp;sr=b&amp;si=downloadResults&amp;sig=MrRxA8o9g8T0X7GyFGvrOMdF%2FEmKr4wT8cqiPCQS6iw%3D" />
</response>

But I am getting the 1st callout response and 2nd callout response are both same , few times it is working fine and few time i am getting both callout has same response.

<?xml version="1.0" encoding="utf-8"?>
<response>
    <task id="5db7594c-721d-4ec4-b922-c07b03d24a76" registrationTime="2017-06-12T11:28:30Z" statusChangeTime="2017-06-12T11:28:30Z" status="Queued" filesCount="1" credits="0" estimatedProcessingTime="5" />
</response>
1

Your code assumes that responseUrl is specified:

public static void GetDownloadUrl(String url){
    // this value can be null            ^
    // so add a null check:
    if (String.isBlank(url)) return;

    // implementation
}
  • Thank you @Adrian Larson, now i got to where the exact issue was. – Mahesh Jun 13 '17 at 7:27
  • I am invoking total 3 callouts, After 1st call out i need to wait for 5seconds to invoke second callout, is there any keyword or method in apex to to delay the function – Mahesh Jun 13 '17 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.