0

So I created a VF Page that utilizes a controller class that I created. Now I need to create the test class for the controller and I am having a hard time with this 'System.NullPointerException: Attempt to de-reference a null object' error. Here is the stack trace of the null error:

'Class.BusinessHoursController.BuildVars: line 101, column 1
Class.BusinessHoursController.<init>: line 66, column 1
Class.BusinessHoursControllerTest.bhTest: line 17, column 1' 

I will display my controller code and then my test class code. Any help would be very appreciated. It errors out my test class when I initialize my controller.

/**************************************************************
@class      BusinessHoursController

@description        Controller for the BusinessHoursEdit Page


@version    2016-09-13  HCA.EB
Created.

**************************************************************/



public class BusinessHoursController {

    public string thelabel {get; set;}
    public sObject therecord {get; set;}
    public String day {get; set;}
    public String sunstartHour { get; set; }
    public String sunstartMinute { get; set; }
    public String sunstartPer { get; set; }
    public String sunendHour { get; set; }
    public String sunendMinute { get; set; }
    public String sunendPer { get; set; }
    public String monstartHour { get; set; }
    public String monstartMinute { get; set; }
    public String monstartPer { get; set; }
    public String monendHour { get; set; }
    public String monendMinute { get; set; }
    public String monendPer { get; set; }
    public String tuestartHour { get; set; }
    public String tuestartMinute { get; set; }
    public String tuestartPer { get; set; }
    public String tueendHour { get; set; }
    public String tueendMinute { get; set; }
    public String tueendPer { get; set; }
    public String wedstartHour { get; set; }
    public String wedstartMinute { get; set; }
    public String wedstartPer { get; set; }
    public String wedendHour { get; set; }
    public String wedendMinute { get; set; }
    public String wedendPer { get; set; }
    public String thurstartHour { get; set; }
    public String thurstartMinute { get; set; }
    public String thurstartPer { get; set; }
    public String thurendHour { get; set; }
    public String thurendMinute { get; set; }
    public String thurendPer { get; set; }
    public String fristartHour { get; set; }
    public String fristartMinute { get; set; }
    public String fristartPer { get; set; }
    public String friendHour { get; set; }
    public String friendMinute { get; set; }
    public String friendPer { get; set; }
    public String satstartHour { get; set; }
    public String satstartMinute { get; set; }
    public String satstartPer { get; set; }
    public String satendHour { get; set; }
    public String satendMinute { get; set; }
    public String satendPer { get; set; }
    public Account record { get; set; }

    //  Retrieves the picklist values from the Enterprise DateTime object
public List<SelectOption> getHourOptions()
{
    return getPicklistOptions(Enterprise_DateTime__c.Hour__c.getDescribe());
}
public List<SelectOption> getMinuteOptions()
{
    return getPicklistOptions(Enterprise_DateTime__c.Minute__c.getDescribe());
}
public List<SelectOption> getPeriodOptions()
{
    return getPicklistOptions(Enterprise_DateTime__c.Period__c.getDescribe());
}


public BusinessHoursController(){
        BuildVars(record);
}


public BusinessHoursController(ApexPages.StandardController stdController)
{
    this.record = (Account)stdController.getRecord();

    BuildVars(this.record);
}

public class BHwrap{
        public String dts {get;set;}
        public String sHr{get;set;}
        public String sMin{get;set;}
        public String sPer{get;set;}
        public String eHr{get;set;}
        public String eMin{get;set;}
        public String ePer{get;set;}

        public BHwrap(String formattedDT){
            this.dts = formattedDT;
        }
    }



    public List<String> days{get;set;}

    public List<BHwrap> bhList
    {
        get;
        set;
    }
    public void BuildVars(Account a){
        List<Account> aq = [Select BHdelimited__c from account where id =:a.id limit 1 ];
        if(aq.size() >0 && aq[0].BHdelimited__c != NULL ){
        List<String> timeSplit = aq[0].BHdelimited__c.split(';');
        integer strCount = 0;


        sunstartHour = timeSplit.get(0);
        sunstartMinute = timeSplit.get(1);
        sunstartPer = timeSplit.get(2).toUpperCase();
        sunendHour = timeSplit.get(3);
        sunendMinute = timeSplit.get(4);
        sunendPer = timeSplit.get(5).toUpperCase();
        monstartHour = timeSplit.get(6);
        monstartMinute = timeSplit.get(7);
        monstartPer = timeSplit.get(8).toUpperCase();
        monendHour = timeSplit.get(9);
        monendMinute = timeSplit.get(10);
        monendPer = timeSplit.get(11).toUpperCase();
        tuestartHour = timeSplit.get(12);
        tuestartMinute = timeSplit.get(13);
        tuestartPer = timeSplit.get(14).toUpperCase();
        tueendHour = timeSplit.get(15);
        tueendMinute = timeSplit.get(16);
        tueendPer = timeSplit.get(17).toUpperCase();
        wedstartHour = timeSplit.get(18);
        wedstartMinute = timeSplit.get(19);
        wedstartPer = timeSplit.get(20).toUpperCase();
        wedendHour = timeSplit.get(21);
        wedendMinute = timeSplit.get(22);
        wedendPer = timeSplit.get(23).toUpperCase();
        thurstartHour = timeSplit.get(24);
        thurstartMinute = timeSplit.get(25);
        thurstartPer = timeSplit.get(26).toUpperCase();
        thurendHour = timeSplit.get(27);
        thurendMinute = timeSplit.get(28);
        thurendPer = timeSplit.get(29).toUpperCase();
        fristartHour = timeSplit.get(30);
        fristartMinute = timeSplit.get(31);
        fristartPer = timeSplit.get(32).toUpperCase();
        friendHour = timeSplit.get(33);
        friendMinute = timeSplit.get(34);
        friendPer = timeSplit.get(35).toUpperCase();
        satstartHour = timeSplit.get(36);
        satstartMinute = timeSplit.get(37);
        satstartPer = timeSplit.get(38).toUpperCase();
        satendHour = timeSplit.get(39);
        satendMinute = timeSplit.get(40);
        satendPer = timeSplit.get(41).toUpperCase();
        }

    }
    public void saveBH()
{
    String datetimeString;
    String dtDelimit;
    days = new List<String>{'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'};

    //This is for the record

    string sundayHours = sunstartHour + ':' + sunstartMinute + sunstartPer.toLowerCase() == sunendHour + ':' + sunendMinute + sunendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+sunstartHour + ':' + sunstartMinute + sunstartPer.toLowerCase()+ ' - '+sunendHour + ':' + sunendMinute + sunendPer.toLowerCase()+' | ' ;
    string mondayHours = monstartHour + ':' + monstartMinute + monstartPer.toLowerCase() == monendHour + ':' + monendMinute + monendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+monstartHour + ':' + monstartMinute + monstartPer.toLowerCase()+ ' - '+monendHour + ':' + monendMinute + monendPer.toLowerCase()+' | ' ;
    string tuesdayHours = tuestartHour + ':' + tuestartMinute + tuestartPer.toLowerCase() == tueendHour + ':' + tueendMinute + tueendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+tuestartHour + ':' + tuestartMinute + tuestartPer.toLowerCase()+ ' - '+tueendHour + ':' + tueendMinute + tueendPer.toLowerCase()+' | ' ;
    string wednesdayHours = wedstartHour + ':' + wedstartMinute + wedstartPer.toLowerCase() == wedendHour + ':' + wedendMinute + wedendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+wedstartHour + ':' + wedstartMinute + wedstartPer.toLowerCase()+ ' - '+wedendHour + ':' + wedendMinute + wedendPer.toLowerCase()+' | ' ;
    string thursdayHours = thurstartHour + ':' + thurstartMinute + thurstartPer.toLowerCase() == thurendHour + ':' + thurendMinute + thurendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+thurstartHour + ':' + thurstartMinute + thurstartPer.toLowerCase()+ ' - '+thurendHour + ':' + thurendMinute + thurendPer.toLowerCase()+' | ' ;
    string fridayHours = fristartHour + ':' + fristartMinute + fristartPer.toLowerCase() == friendHour + ':' + friendMinute + friendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+fristartHour + ':' + fristartMinute + fristartPer.toLowerCase()+ ' - '+friendHour + ':' + friendMinute + friendPer.toLowerCase()+' | ' ;
    string saturdayHours = satstartHour + ':' + satstartMinute + satstartPer.toLowerCase() == satendHour + ':' + satendMinute + satendPer.toLowerCase() ? ' : Closed | ' : 
                     ': '+satstartHour + ':' + satstartMinute + satstartPer.toLowerCase()+ ' - '+satendHour + ':' + satendMinute + satendPer.toLowerCase()+' | ' ;

    datetimeString = days.get(0)+ sundayHours;
    dtDelimit = sunstartHour + ';' + sunstartMinute +';'+ sunstartPer.toLowerCase()+ ';'+sunendHour + ';' + sunendMinute +';'+ sunendPer.toLowerCase()+';';
    datetimeString += days.get(1)+ mondayHours;
    dtDelimit +=monstartHour + ';' + monstartMinute +';'+ monstartPer.toLowerCase()+ ';'+monendHour + ';' + monendMinute +';'+ monendPer.toLowerCase()+';';
    datetimeString += days.get(2)+ tuesdayHours;
    dtDelimit += tuestartHour + ';' + tuestartMinute +';'+ tuestartPer.toLowerCase()+ ';'+tueendHour + ';' + tueendMinute +';'+ tueendPer.toLowerCase()+';';
    datetimeString += days.get(3)+ wednesdayHours;
    dtDelimit += wedstartHour + ';' + wedstartMinute +';'+ wedstartPer.toLowerCase()+ ';'+wedendHour + ';' + wedendMinute +';'+ wedendPer.toLowerCase()+';';
    datetimeString += days.get(4)+ thursdayHours;
    dtDelimit += thurstartHour + ';' + thurstartMinute +';'+ thurstartPer.toLowerCase()+ ';'+thurendHour + ';' + thurendMinute +';'+ thurendPer.toLowerCase()+';';
    datetimeString += days.get(5)+ fridayHours;
    dtDelimit += fristartHour + ';' + fristartMinute +';'+ fristartPer.toLowerCase()+ ';'+friendHour + ';' + friendMinute +';'+ friendPer.toLowerCase()+';';
    datetimeString += days.get(6)+ saturdayHours ;
    dtDelimit +=satstartHour + ';' + satstartMinute +';'+ satstartPer.toLowerCase()+ ';'+satendHour + ';' + satendMinute +';'+ satendPer.toLowerCase();

    //This is for the wrapper
    system.debug('DateTimeString is: '+ datetimeString);
    system.debug('thelabel is: '+ thelabel);

        record.Business_Hours__c = datetimeString;
        record.BHdelimited__c = dtDelimit;
        checkDateTimes();

// if there are no errors then update the record 
    if (!ApexPages.hasMessages())
    {

        try {
                saveAcct();
            } 
        catch(DmlException excp) {
            ApexPages.addMessages(excp);
        }
    }


}
public PageReference saveAcct()
{
    system.debug('This is the record ID: '+ record.id);
    upsert record;
        PageReference reference = new PageReference('/'+record.id);
        reference.setRedirect( true );
        return reference;   

}


/*******************************************************
        checkDateTimes()

Checks for validation errors in the start/end datetimes
*******************************************************/

public void checkDateTimes() {
    if (record.Business_Hours__c.contains('-Hr-')){
        ApexPages.addMessage( new ApexPages.Message( ApexPages.Severity.ERROR, 'There are empty time slots in the Business Hours. You must enter a value.' ) ); 
    } 
    if (record.Business_Hours__c.contains('-Min-')){
        ApexPages.addMessage( new ApexPages.Message( ApexPages.Severity.ERROR, 'There are empty time slots in the Business Hours. You must enter a value.' ) );
    }
    if (record.Business_Hours__c.contains('-per-')){
        ApexPages.addMessage( new ApexPages.Message( ApexPages.Severity.ERROR, 'There are empty time slots in the Business Hours. You must enter a value.' ) );
    }
}   

 /*******************************************************
        clearHours

Called by the component to clear the current object's
date/time fields
*******************************************************/
public void clearHours()
{

    sunendHour = null;
    monendHour = null;
    tueendHour = null;
    wedendHour = null;
    thurendHour = null;
    friendHour = null;
    satendHour = null;
    sunendMinute = null;
    monendMinute = null;
    tueendMinute = null;
    wedendMinute = null;
    thurendMinute = null;
    friendMinute = null;
    satendMinute = null;
    sunendPer = null;
    monendPer = null;
    tueendPer = null;
    wedendPer = null;
    thurendPer = null;
    friendPer = null;
    satendPer = null;
    sunstartHour = null;
    monstartHour = null;
    tuestartHour = null;
    wedstartHour = null;
    thurstartHour = null;
    fristartHour = null;
    satstartHour = null;
    sunstartMinute = null;
    monstartMinute = null;
    tuestartMinute = null;
    wedstartMinute = null;
    thurstartMinute = null;
    fristartMinute = null;
    satstartMinute = null;
    sunstartPer = null;
    monstartPer = null;
    tuestartPer = null;
    wedstartPer = null;
    thurstartPer = null;
    fristartPer = null;
    satstartPer = null;

}





/*******************************************************
        getPicklistOptions

Utility method used by the methods above to retrieve the picklist values from
the schema for a given field and returns them as a list of select options
*******************************************************/
private List<SelectOption> getPicklistOptions(Schema.DescribeFieldResult fieldResult)
{
    List<SelectOption> sol = new List<SelectOption>();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    for ( Schema.PicklistEntry f : ple )
    {
        sol.add( new SelectOption( f.getLabel(), f.getValue() ) );
    }

    return sol;
}

}

Here is my Test Class:

/********************************************
@class      BusinessHoursControllerTest

@brief      Test Class for BusinessHoursController


@version    2016-09-26  HCA.EB
Created.
******************************************/
@isTest
private class BusinessHoursControllerTest {

    static testMethod void bhTest(){

        //  Initialize the controller
    BusinessHoursController bhc = new BusinessHoursController();




    /*******************************************************
        Test the setters/getters
    *******************************************************/
    bhc.getHourOptions();
    bhc.getMinuteOptions();
    bhc.getperiodOptions();



    /*******************************************************
        Test saveBH()
    *******************************************************/

    //  Create an Facility
    Account theFacility = TestUtils.createFacility();
    insert theFacility;
    Account a = [Select id,BHdelimited__c from Account WHERE id=:theFacility.id LIMIT 1];
    bhc.days = new List<String>{'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'};

    bhc.record = a;
    bhc.sunstartHour = '12';
    bhc.sunstartMinute = '12';
    bhc.sunstartPer = 'am';
    bhc.sunendHour = '12';
    bhc.sunendMinute = '12';
    bhc.sunendPer = 'pm';
    bhc.monstartHour= '12';
    bhc.monstartMinute= '12';
    bhc.monstartPer= 'am';
    bhc.monendHour= '12';
    bhc.monendMinute= '12';
    bhc.monendPer= 'am';
    bhc.tuestartHour= '12';
    bhc.tuestartMinute= '12';
    bhc.tuestartPer= 'am';
    bhc.tueendHour= '12';
    bhc.tueendMinute= '12';
    bhc.tueendPer= 'pm';
    bhc.wedstartHour= '12';
    bhc.wedstartMinute= '12';
    bhc.wedstartPer= 'am';
    bhc.wedendHour= '12';
    bhc.wedendMinute= '12';
    bhc.wedendPer= 'pm';
    bhc.thurstartHour= '12';
    bhc.thurstartMinute= '12';
    bhc.thurstartPer= 'am';
    bhc.thurendHour= '12';
    bhc.thurendMinute= '12';
    bhc.thurendPer= 'pm';
    bhc.fristartHour= '12';
    bhc.fristartMinute= '12';
    bhc.fristartPer= 'am';
    bhc.friendHour= '12';
    bhc.friendMinute= '12';
    bhc.friendPer= 'pm';
    bhc.satstartHour= '12';
    bhc.satstartMinute= '12';
    bhc.satstartPer= 'am';
    bhc.satendHour= '12';
    bhc.satendMinute= '12';
    bhc.satendPer= 'pm';

    bhc.saveBH();


}
}
1

Here you are passing account instance

BuildVars(record); // it is still null

But you haven't initialized it yet.

So in this query you will get null pointer error

List<Account> aq = [Select BHdelimited__c from account where id =:a.id limit 1 ];

You can do one thing here create a account record and a standard controller instance it use it

Account theFacility = TestUtils.createFacility();
insert theFacility;
ApexPages.StandardController stdController = new ApexPages.StandardControlle(theFacility);

and update this to

BusinessHoursController bhc = new BusinessHoursController(stdController);

Alo I suggest you to check Apex testing on Trailhead

0

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.