1

I'm a new admin in an org that has had some custom development done in the past and our code coverage has fallen below 75%. In one of our Apex Test Classes, we get an error that states:

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

I know I either need to set a variable to a static value in the test class or change the class to check for null, unfortunately I'm very green when it comes to Apex and have no idea how to address this.

Any help is greatly appreciated!

Error Message: Stack Trace: Class.scheduledTrucks_Extension.verifyQty: line 138, column 1 Class.scheduledTrucks_Extension.doQueries: line 91, column 1 Class.scheduledTrucks_Extension.: line 33, column 1 Class.scheduledTrucks_Extension_Test.scheduledTrucks_Extension_Test: line 7, column 1

Extension

public with sharing class scheduledTrucks_Extension {
public Fulfillment__c fulfillment {get;set;}

public string fulfillmentID {get;set;}
public list<selectoption> fulfillmentoptions {get;set;}
public list<lineitemwrapper> liwlist {get;set;}

public boolean needtoselect {get;set;}
public boolean shipped {get;set;}

public boolean qtverifyneeded {get;set;}
public boolean qtverified {get;set;}
public string qtmessage {get;set;}

public class lineitemwrapper {
    public Fulfillment_Line_Item__c lineitem {get;set;}
    public list<Fulfillment_Lot_Allocation__c> lotallocations {get;set;}

    lineitemwrapper (Fulfillment_Line_Item__c li) {
        lineitem=li;
    }
}

public scheduledTrucks_Extension (apexpages.standardcontroller controller) {
    fulfillmentoptions = new list<selectoption>();
    liwlist = new list<lineitemwrapper>();
    fulfillment = (Fulfillment__c)controller.getrecord(); 

    if (fulfillment.id != null) {
        fulfillmentid = fulfillment.id; //for page render controlling
        fulfillment = [Select f.Value__c, f.Trailer__c, f.Total_Replant_Lbs__c, f.Total_Lbs__c, f.Total_Allocated_Lbs__c, f.SystemModstamp, f.Status__c, f.Special_Instructions__c, f.Ship_to_Address__c, f.Ship_To__c, f.Ship_To__r.Name, f.Seal_No__c, f.Sales_Order__c, f.Required_Date_del__c, f.RecordTypeId, f.Reason__c, f.Rcvr_Ref__c, f.Rcvr_Comments__c, f.Quote__c, f.Quote_No__c, f.Product__c, f.Pick_Up__c, f.OwnerId, f.Override__c, f.Origin_Location__c, f.Order_Ref__c, f.Name, f.MA__c, f.Location__c, f.LastModifiedDate, f.LastModifiedById, f.LastActivityDate, f.IsDeleted, f.Id, f.Fulfillment_Record_Type_Name__c, f.Frt_Comment__c, f.Frt_Carrier__c, f.Frt_Bill_To__c, f.Freight_Terms__c, f.Destination__c, f.Date_Shipped__c, f.Customer__c, f.CreatedDate, f.CreatedById, f.Contract__c, f.Cont_Acres__c, f.Check_Number__c, f.Carrier_Phone__c, f.Bulk_Transactions__c, f.Bulk_IC_Override__c, f.Booking__c, f.Bill_To__c, f.Balance__c, f.BOL__c, f.Amt_Paid__c From Fulfillment__c f where f.id = :fulfillmentID]; 
        if (test.isRunningTest() || fulfillment.Status__c == 'Truck Scheduled') {
            doQueries();
        }
        else {
            Apexpages.addmessage(new apexpages.message(apexpages.severity.info,'This fulfillment does not currently have a truck scheduled'));
        }
    }
    else {
        fulfillmentoptions.add(new selectoption('','Select a scheduled fulfillment.'));
        for (fulfillment__c f : [select id,name from fulfillment__c where status__c = 'Truck Scheduled' order by name]) {
            fulfillmentoptions.add(new selectoption(f.id,f.name));
        }
        needtoselect = true;
    }
}

public pagereference fulfillmentselected() {
    fulfillment = [Select f.Value__c, f.Trailer__c, f.Total_Replant_Lbs__c, f.Total_Lbs__c, f.Total_Allocated_Lbs__c, f.SystemModstamp, f.Status__c, f.Special_Instructions__c, f.Ship_to_Address__c, f.Ship_To__c, f.ship_to__r.Name, f.Seal_No__c, f.Sales_Order__c, f.Required_Date_del__c, f.RecordTypeId, f.Reason__c, f.Rcvr_Ref__c, f.Rcvr_Comments__c, f.Quote__c, f.Quote_No__c, f.Product__c, f.Pick_Up__c, f.OwnerId, f.Override__c, f.Origin_Location__c, f.Order_Ref__c, f.Name, f.MA__c, f.Location__c, f.LastModifiedDate, f.LastModifiedById, f.LastActivityDate, f.IsDeleted, f.Id, f.Fulfillment_Record_Type_Name__c, f.Frt_Comment__c, f.Frt_Carrier__c, f.Frt_Bill_To__c, f.Freight_Terms__c, f.Destination__c, f.Date_Shipped__c, f.Customer__c, f.CreatedDate, f.CreatedById, f.Contract__c, f.Cont_Acres__c, f.Check_Number__c, f.Carrier_Phone__c, f.Bulk_Transactions__c, f.Bulk_IC_Override__c, f.Booking__c, f.Bill_To__c, f.Balance__c, f.BOL__c, f.Amt_Paid__c From Fulfillment__c f where f.id = :fulfillmentID];
    if (test.isRunningTest() || fulfillment.Status__c == 'Truck Scheduled') {
        doQueries();
        needtoselect=false;
    }
    else {
        Apexpages.addmessage(new apexpages.message(apexpages.severity.info,'This fulfillment does not currently have a truck scheduled'));
    }
    return null;
}

public pagereference doQueries (){ 
    list<Fulfillment_Lot_Allocation__c> lots = new list<Fulfillment_Lot_Allocation__c>();
    list<Fulfillment_Line_Item__c> flis = new list<Fulfillment_Line_Item__c>();
    set<id> lineitemIDS = new set<id>();
    for (Fulfillment_Line_Item__c fli : [Select f.Value__c, f.Unknown_Lots__c, f.UOM__c, f.Total_Lbs__c, f.SystemModstamp, f.Status__c, f.Shipped__c, f.Service_Agreement__c, f.Seal_No__c, f.Sales_Order__c, f.SO_Status__c, f.SO_Line_Item__c, f.Replant__c, f.RecordTypeId, f.Quantity__c, f.Qty_Allocated__c, f.Product__c, f.product__r.name, f.Price_USD_Lb__c, f.Pallets__c, f.PO__c, f.Origin_Location__c, f.Name, f.Location__c, f.Lbs_Override__c, f.LastModifiedDate, f.LastModifiedById, f.LastActivityDate, f.IsDeleted, f.Invoice__c, f.Id, f.Fulfillment__c, f.Freight_Terms__c, f.Final_Lbs__c, f.Date_Shipped__c, f.Date_Loaded__c, f.Customer__c, f.CreatedDate, f.CreatedById, f.Container_No__c, f.CC_Fee__c, f.BOL__c, f.BOL1__c, f.Allocated_Lbs__c From Fulfillment_Line_Item__c f where f.fulfillment__c = :fulfillment.id]) {
        flis.add(fli);
        lineitemIDS.add(fli.id);
        lineitemwrapper liw = new lineitemwrapper(fli);
        liwlist.add(liw);           
    }

    map<id,list<Fulfillment_Lot_Allocation__c>> lilas = new map<id,list<Fulfillment_Lot_Allocation__c>>();
    list<Fulfillment_Lot_Allocation__c> templist = new list<Fulfillment_Lot_Allocation__c>();
    for (Fulfillment_Lot_Allocation__c fla : [Select f.SystemModstamp, f.Shipped__c, f.Salmonella__c, f.RecordTypeId, f.Rcvd_Qty_Lbs__c, f.Quantity_Lbs__c, f.Qty_Units__c, f.Purity_wt__c, f.Purity__c, f.Products__c, f.Pallet_No__c, f.Name, f.Moisture__c, f.Moist_wt__c, f.Lot_Value__c, f.Location__c, f.LastModifiedDate, f.LastModifiedById, f.IsDeleted, f.Id, f.Fulfillment_Number__c, f.Fulfillment_Line_Item__c, f.Fstatus__c, f.FG_Lots__c, f.fg_lots__r.name, f.E_Coli__c, f.Date_shipped__c, f.Customer__c, f.Customer_ID__c, f.CreatedDate, f.CreatedById, f.Bulk_IC_Override__c, f.BOL__c From Fulfillment_Lot_Allocation__c f Where Fulfillment_Line_Item__c in :lineitemIDS]) {
        if (lilas.containskey(fla.fulfillment_line_item__c) == true) {
            templist = lilas.get(fla.fulfillment_line_item__c);
            templist.add(fla);
            lilas.put(fla.fulfillment_line_item__c,templist);
        }
        else {
            templist = new list<fulfillment_lot_allocation__c>();
            templist.add(fla);
            lilas.put(fla.fulfillment_line_item__c,templist);
        }
    }

    for (lineitemwrapper liw : liwlist) {
        if (lilas.containskey(liw.lineitem.id) == true) {
            liw.lotallocations = lilas.get(liw.lineitem.id);
        }
    }
    verifyqty();
    return null;
}

public pagereference saveAndShip () {
    boolean pass;
    verifyqty();
    if (qtverifyneeded == true && qtverified == false) {
        pass = false;
    }
    else {
        pass=true;
    }

    if (pass == false) {
        apexpages.addmessage(new apexpages.message(apexpages.severity.error,'You must verify that the quantity is exactly 50,000 lbs.'));
        return null;
    }
    else {
        list<Fulfillment_Lot_Allocation__c> updatefla = new list<Fulfillment_Lot_Allocation__c>();
        try {

            for (lineitemwrapper liw : liwlist) {
                for (Fulfillment_Lot_Allocation__c fla : liw.lotallocations) {
                    //fla.shipped__c = true;
                    updatefla.add(fla);
                }
            }
            update updatefla;

            fulfillment.status__c = 'Shipped';
            update fulfillment;
            fulfillment_methods.processShippedFulfillments(new set<id>{fulfillment.id}); // trigger was not firing for some reason ...

            shipped=true;
            apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'Shipment successful'));
        }
        catch (exception e) {
            apexpages.addmessage(new apexpages.message(apexpages.severity.error,'There was an error processing your shipment.'));
        }
    }
    return null;
}

public pagereference verifyQty (){
    qtverifyneeded=false;
    for (lineitemwrapper liw : liwlist) {
        for (Fulfillment_Lot_Allocation__c fla : liw.lotallocations) {
            if (fla.quantity_lbs__c == 50000) {
                qtmessage = 'Please verify that the qty is exactly 50,000 or change qty.';
                qtverifyneeded=true; //this lineitem requires verification
            }   
        }
    }
    return null;
} 
}

Test Class:

@isTest (seealldata=true)
private class scheduledTrucks_Extension_Test {
public static testmethod void scheduledTrucks_Extension_Test () {
    Fulfillment__c f = [select id from fulfillment__c order by createddate desc limit 1];

    apexpages.standardcontroller cont = new apexpages.standardcontroller(f);
    scheduledTrucks_Extension ext = new scheduledTrucks_Extension(cont);

    ext.
    ext.fulfillmentselected();
    ext.qtverified = true;
    ext.saveAndShip();
}   //so code format will pass
}
2
  • 1
    What is at Class.scheduledTrucks_Extension.verifyQty: line 138, is it for (lineitemwrapper liw : liwlist) {? If so add a line above it that is if(liwlist==null) return null; This does not solve the issue of ensuring business requirements are met though but should resolve the DNO error – Eric Nov 7 '14 at 20:04
  • 1
    In case the problem is with this line for (Fulfillment_Lot_Allocation__c fla : liw.lotallocations) { it would be safer if you always initialise the list using lotallocations = new list<Fulfillment_Lot_Allocation__c>(); in the lineitemwrapper constructor. – Keith C Nov 7 '14 at 20:22
1

If line 138 is indeed this (as it appears to be):

for (Fulfillment_Lot_Allocation__c fla : liw.lotallocations) {

The problem is that lotallocations is null. The issue can be simply corrected by initializing the lotallocations list when the lineitemwrapper instance is constructed so that the variable is never null:

public class lineitemwrapper {
    public Fulfillment_Line_Item__c lineitem {get;set;}
    public list<Fulfillment_Lot_Allocation__c> lotallocations {get;set;}

    lineitemwrapper (Fulfillment_Line_Item__c li) {
        lineitem=li;
        // initialize the variable as an empty list
        lotallocations = new List<Fulfillment_Lot_Allocation>();
    }
}
1
  • Awesome, thanks Mark! – Sesaco Admin Nov 10 '14 at 14:42

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.