0

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.setMethod('GET');
        if(!Test.isRunningTest()){
            req.setEndpoint('http://www.boxoh.com/?t=' + (String)o.get('Tracking__c'));
            req.setMethod('GET');
            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');
        System.debug(xmlcut[40]);
        String XmlProcessed = xmlcut[40];
        XmlProcessed = XmlProcessed.replace(' ', '');
        XmlProcessed = XmlProcessed.substring(53);
        String XmlDualProcessed = XmlProcessed.split('<')[0];
        System.debug(XmlProcessed);
        System.debug(XmlDualProcessed);
        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.

JNK
  • 3
  • 2

1 Answers1

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.

Simon Lawrence
  • 6,766
  • 5
  • 34
  • 57