0

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

class1:

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);
    }
}

Class2:

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);

    }

}

I am getting attempt to de-reference null object error in the second class can anyone please help me , how to fix this?

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

        // ??
    }
}
Daniel Ballinger
  • 102,288
  • 39
  • 270
  • 594
Mahesh
  • 43
  • 1
  • 4
  • 13
  • Few times it is working fine, but most of the time it is giving error has attempt to deference null objcet – Mahesh Jun 12 '17 at 10:24
  • 2
    You need to identify the line number the problem occurs at - see e.g. https://salesforce.stackexchange.com/questions/36582/how-do-i-start-to-debug-my-own-apex-code for how to get that information - and then consider what null value on that line is causing the problem. – Keith C Jun 12 '17 at 10:43
  • 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)|

    – Mahesh Jun 12 '17 at 10:50
  • Check line 7 of GetLeadfromUrl.GetDownloadUrl which is where the problem originates. – Keith C Jun 12 '17 at 11:14
  • 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;
    
    – Mahesh Jun 12 '17 at 11:19

1 Answers1

1

It would appear the parameter being passed into GetLeadfromUrl.GetDownloadUrl(String url) is null.

The first thing you can do is guard against this as early as possible in the method.

E.g.

public class GetLeadfromUrl {
    public static void GetDownloadUrl(string url) { 
        System.assertNotEquals(null, url, 'url parameter is required');
        String ROOT_TAG = 'ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd';
    }
}

Because the url parameter is null we can imply that resulturl is also null.

String resulturl = doc.getRootElement().getChildElement('task', null).getAttribute('resultUrl', null);

Check your debug logs that the XML you are trying to parse matches your expectations.

Daniel Ballinger
  • 102,288
  • 39
  • 270
  • 594