I am running tests on the following code:

global class CarrierTrackingBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{

global String ShippingStatus;
global String query;
global String ShipmentStatus;
global Boolean constant;
global String savedShipping;
global String XmlReturn;
public static String trackingReturn;

global Database.QueryLocator start(Database.BatchableContext BC){
   String Delivered = 'Delivered';
   String query = 'select Tracking__c,Shipping_Status__c from ShippingTest__c WHERE Shipping_Date__c = LAST_N_DAYS:18';
   String ShippingStatus = 'Shipping_Status__c';
   return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC, List<sObject> scope){
    for(sObject o : scope){
        constant = false;
        trackingReturn = (String)o.get('Tracking__c');
        if ((String)o.get('Shipping_Status__c') == 'Delivered'){
            constant = true;
            savedShipping = (String)o.get('Shipping_Status__c');
        } else {
            constant = false;
        HttpResponse res;
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://www.boxoh.com/?t=' + (String)o.get('Tracking__c'));
            req.setEndpoint('http://www.boxoh.com/?t=' + (String)o.get('Tracking__c'));
            Http http = new Http();
            res = http.send(req);
            System.debug('Not a test');
        } else {
            System.assertEquals('http://www.boxoh.com/?t=' + (String)o.get('Tracking__c'), req.getEndpoint());
            System.assertEquals('GET', req.getMethod());
            String nullline = '\\n';
            String nulllinerepeated = nullline.repeat(39);
            String filler = '<';
            String fillerrepeated = filler.repeat(49);
            res.setBody(nulllinerepeated + fillerrepeated + 'Delivered<no>');
            System.debug('A test');
        XmlStreamReader reader = res.getXmlStreamReader();
        String XmlReturn = res.getBody();
        XmlReturn = XmlReturn.replace('<',' <');
        XmlReturn = XmlReturn.replace('>','> ');
        String[] xmlcut = XmlReturn.split('\\n');
        String XmlProcessed = xmlcut[40];
        XmlProcessed = XmlProcessed.replace(' ', '');
        XmlProcessed = XmlProcessed.substring(53);
        String XmlDualProcessed = XmlProcessed.split('<')[0];
        String ShipmentStatus = '';
        ShipmentStatus = XmlDualProcessed;
        o.put('Shipping_Status__c', ShipmentStatus);
        if (constant == true){
        o.put('Shipping_Status__c', savedShipping);
        } else {
            o.put('Shipping_Status__c', ShipmentStatus);

    update scope;


global void finish(Database.BatchableContext BC){


I am getting an error stating:

System.NullPointerException: Attempt to de-reference a null object

I am familiar with this error but I cannot see why I am getting it. It is referenced towards line 45, which is

res.setBody(nulllinerepeated + fillerrepeated + 'Delivered<no>');

I am just confused.

  • It looks like the only other possibility is that your res variable is null. Can you edit your post to provide more of your code so that we can see where/how res is being declared & defined?
    – Derek F
    Commented Dec 30, 2015 at 16:23
  • Does that help?
    – JNK
    Commented Dec 30, 2015 at 16:26
  • Yes, it does indeed.
    – Derek F
    Commented Dec 30, 2015 at 16:29
  • Damn it @DerekF solved it in the comments before I could hit submit on my answer again! Guy is a speed-demon ;) Commented Dec 30, 2015 at 16:32
  • I'm not normally so quick. Must've rolled out of bed with +5 agility today.
    – Derek F
    Commented Dec 30, 2015 at 16:35

1 Answer 1


Your HttpResponse object is still null, as you are not instantiating it.

After line 28 you need to do something like

res = new HttpResponse();

or indeed make line 28:

HttpResponse res = new HttpResponse();

to get the ball rolling.

  • Are there still issues with tests for batch apex using a helper class that implements the HttpCalloutMock interface as per Testing HTTP Callouts by Implementing the HttpCalloutMock Interface? The best information I could find is in the comments on this accepted answer on another question. If mocking the callout is viable, OP would be able to remove the else block (and the assertions into the test class)
    – Derek F
    Commented Dec 30, 2015 at 16:51
  • @DerekF You should be able to mock the callout properly. I had a helper class that was used to perform mock responses that could be controlled from the unit test itself. The accepted answer you linked to is two years old, and definitely dated.
    – sfdcfox
    Commented Dec 30, 2015 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .