function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
p100p100 

Dereference null object - setting List<String> in test class

Hi guys

 

I've set up a report as a custom VF page, with a multiselect field that controls the data that is displayed. All works fine. However, im struggling with setting the List<String> in the test class, i keep getting the 'attempt to dereference a null object' error on the following line in the test class. Any ideas where i'm going wrong? Controllor and test class below.

Thanks for any suggestions.

 

public class RAMSuccessTrackerController {

    public List<Site__c> Records {get;set;}
    public double TotalSales {get;set;}
    public double averageRoce {get;set;}
    public List<Site__c> Totals {get;set;}
    public double averageNPVGI {get;set;}
    public List<String> surveyor { get; set; }
    public integer iBWS {get;set;}
    public integer iLegals {get;set;}
    public integer iPlanning {get;set;}
    
    public PageReference refreshList()
    {
        system.debug(surveyor);
        Records = [Select Site_Reference_Number__c, Name,Surveyor__r.Name, Postcode__c, TypeOfSite__c, Status__c, Launch_Date__c,  GovtRegion__c,Rent__c,TotalSalesPost__c,  
                        (Select MeetingDate__c, RecordType.Name,  TotalSalesPost__c, NPV__c, GrossInvestment__c,NPVGrossInvestment__c, CPAR__c, Tenure__c, Rent__c,SalesAreaPostExcCO__c,ROCE__c  
                            From Financial__r 
                            Where MeetingDate__c <> null and RecordType.Name in ('CCM','CCM Update') 
                            order by MeetingDate__c  desc limit 1), 
                                (Select Risk__c,Launch_Date__c,Launch_FY__c,Launch_Quarter__c, Start_On_Site__c, SDG1__c, SDG2CDG2__c, SDG2R__c, SDG3CDG3__c,aBWS_Licence_Granted__c,aLegalsExchanged__c,  ProjectManager__c, status__c,aObtainPlanningPermission__c 
                                From Investment_Programs__r 
                                where status__c in ('Live','Feasibility','Completed') and Risk__c in('0','25','50','75','100') and Launch_FY__c in ('13/14','14/15')), 
                                InvestmentType__c  
                                From Site__c 
                                	where ID in (Select Site__c 
                                				 from Financial__c 
                                				 	where Recordtype.Name In('CCM','CCM Update'))
                                                    and ID in (Select Site__c from InvestmentProgram__c 
                                                                   where status__c in ('Live','Feasibility','Completed') and Risk__c in('0','25','50','75','100') and Launch_FY__c in ('13/14','14/15')) and Surveyor__c in :surveyor 
                                                                   order by surveyor__c, Launch_Date__c asc limit 999];
         TotalSales = 0;
         averageRoce = 0;
         integer i = 0;  
         double totalNPV = 0;
         double totalGI = 0;
         iBWS = 0;
         iPlanning = 0;
         iLegals = 0;
                                                                
         for(Site__c totals : Records)
         {
             if(totals.Financial__r[0].TotalSalesPost__c != null)
             {
                 TotalSales = TotalSales + totals.Financial__r[0].TotalSalesPost__c;
             }
             if(totals.Financial__r[0].Roce__c != null)
             {
                 averageRoce = averageRoce + totals.Financial__r[0].Roce__c;
                 i++;
             }
             if(totals.Financial__r[0].NPV__c != null && totals.Financial__r[0].GrossInvestment__c != null)
             {
                 totalNPV = TotalNPV + totals.Financial__r[0].NPV__c;
                 totalGI = TotalGI + totals.Financial__r[0].GrossInvestment__c ;
             }
             if(totals.Investment_Programs__r[0].aBWS_Licence_Granted__c != null)
             {
                 iBWS++;
             }
             if(totals.Investment_Programs__r[0].aLegalsExchanged__c != null)
             {
                 iLegals++;
             }
             if(totals.Investment_Programs__r[0].aObtainPlanningPermission__c != null)
             {
                 iPlanning++;
             }
         } 
         averageRoce = averageRoce/i;
         averageNPVGI = (TotalNPV/totalGI)*100;
        return null;
    }
    
    public RAMSuccessTrackerController ()
    {


    }
    public List<SelectOption> getSurveyors()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        for(User users : [Select id, Name from User where Profile.Name = 'Surveyor' and isActive = true  order by Name])
        {
            options.add(new selectOption(users.Id, users.Name));
        }
        
        return options;
    }
}

Test Class

@isTest
private class RAMSuccessTrackerControllerTest {

	
    static testMethod void TestRefresh() {
    	
    	//create data
    	Site__c newSite = TestClassHelper.insertSite();
    	Financial__c newFin = TestClassHelper.insertFinancialRecord(newSite.Id, 'CCM');
    	InvestmentProgram__c newIP = new InvestmentProgram__c();
    	newIP.Site__c = newSite.ID;
    	newIP.PlannedReLaunchDate__c = system.today();
    	newIP.Surveyor__c = newSite.Surveyor__c;
    	 
    	system.debug('SURV --------' + newSite.Surveyor__c);
    	PageReference pg = Page.RAMSuccessTracker;
    	Test.setCurrentPage(pg);
    	RAMSuccessTrackerController con = new RAMSuccessTrackerController();
    	con.getSurveyors();
    	
    	List<User> surveyorUser = [Select name from User where ProfileId =:SystemUtilities.getProfileId(SystemConstants.PROFILE_SURVEYOR) limit 1];
		system.debug('LIST _____ ' + surveyorUser);
		
		String s = '';
		for(User u: surveyorUser)
		{
			s = string.valueOf(u.Name);
		}
		
		con.surveyor.add(s);
    	con.refreshList();
      
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Well you never initialised the variable

Have a look at the updated code below

 

public class RAMSuccessTrackerController {

    public List<Site__c> Records {get;set;}
    public double TotalSales {get;set;}
    public double averageRoce {get;set;}
    public List<Site__c> Totals {get;set;}
    public double averageNPVGI {get;set;}
    public List<String> surveyor { get; set; }
    public integer iBWS {get;set;}
    public integer iLegals {get;set;}
    public integer iPlanning {get;set;}
    
    public PageReference refreshList()
    {
        system.debug(surveyor);
        Records = [Select Site_Reference_Number__c, Name,Surveyor__r.Name, Postcode__c, TypeOfSite__c, Status__c, Launch_Date__c,  GovtRegion__c,Rent__c,TotalSalesPost__c,  
                        (Select MeetingDate__c, RecordType.Name,  TotalSalesPost__c, NPV__c, GrossInvestment__c,NPVGrossInvestment__c, CPAR__c, Tenure__c, Rent__c,SalesAreaPostExcCO__c,ROCE__c  
                            From Financial__r 
                            Where MeetingDate__c <> null and RecordType.Name in ('CCM','CCM Update') 
                            order by MeetingDate__c  desc limit 1), 
                                (Select Risk__c,Launch_Date__c,Launch_FY__c,Launch_Quarter__c, Start_On_Site__c, SDG1__c, SDG2CDG2__c, SDG2R__c, SDG3CDG3__c,aBWS_Licence_Granted__c,aLegalsExchanged__c,  ProjectManager__c, status__c,aObtainPlanningPermission__c 
                                From Investment_Programs__r 
                                where status__c in ('Live','Feasibility','Completed') and Risk__c in('0','25','50','75','100') and Launch_FY__c in ('13/14','14/15')), 
                                InvestmentType__c  
                                From Site__c 
                                	where ID in (Select Site__c 
                                				 from Financial__c 
                                				 	where Recordtype.Name In('CCM','CCM Update'))
                                                    and ID in (Select Site__c from InvestmentProgram__c 
                                                                   where status__c in ('Live','Feasibility','Completed') and Risk__c in('0','25','50','75','100') and Launch_FY__c in ('13/14','14/15')) and Surveyor__c in :surveyor 
                                                                   order by surveyor__c, Launch_Date__c asc limit 999];
         TotalSales = 0;
         averageRoce = 0;
         integer i = 0;  
         double totalNPV = 0;
         double totalGI = 0;
         iBWS = 0;
         iPlanning = 0;
         iLegals = 0;
                                                                
         for(Site__c totals : Records)
         {
             if(totals.Financial__r[0].TotalSalesPost__c != null)
             {
                 TotalSales = TotalSales + totals.Financial__r[0].TotalSalesPost__c;
             }
             if(totals.Financial__r[0].Roce__c != null)
             {
                 averageRoce = averageRoce + totals.Financial__r[0].Roce__c;
                 i++;
             }
             if(totals.Financial__r[0].NPV__c != null && totals.Financial__r[0].GrossInvestment__c != null)
             {
                 totalNPV = TotalNPV + totals.Financial__r[0].NPV__c;
                 totalGI = TotalGI + totals.Financial__r[0].GrossInvestment__c ;
             }
             if(totals.Investment_Programs__r[0].aBWS_Licence_Granted__c != null)
             {
                 iBWS++;
             }
             if(totals.Investment_Programs__r[0].aLegalsExchanged__c != null)
             {
                 iLegals++;
             }
             if(totals.Investment_Programs__r[0].aObtainPlanningPermission__c != null)
             {
                 iPlanning++;
             }
         } 
         averageRoce = averageRoce/i;
         averageNPVGI = (TotalNPV/totalGI)*100;
        return null;
    }
    
    public RAMSuccessTrackerController ()
    {

        surveyor = new List<String>();
    }
    public List<SelectOption> getSurveyors()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        for(User users : [Select id, Name from User where Profile.Name = 'Surveyor' and isActive = true  order by Name])
        {
            options.add(new selectOption(users.Id, users.Name));
        }
        
        return options;
    }
}

All Answers

Avidev9Avidev9
I guess you would also have the line number!
Please check the full error message and highlight the line number.

This will help members to pin point the issue.
p100p100

That might be useful.... sorry!

 

it happens on the following line in the test class:

 

con.surveyor.add(s);
Avidev9Avidev9

Well you never initialised the variable

Have a look at the updated code below

 

public class RAMSuccessTrackerController {

    public List<Site__c> Records {get;set;}
    public double TotalSales {get;set;}
    public double averageRoce {get;set;}
    public List<Site__c> Totals {get;set;}
    public double averageNPVGI {get;set;}
    public List<String> surveyor { get; set; }
    public integer iBWS {get;set;}
    public integer iLegals {get;set;}
    public integer iPlanning {get;set;}
    
    public PageReference refreshList()
    {
        system.debug(surveyor);
        Records = [Select Site_Reference_Number__c, Name,Surveyor__r.Name, Postcode__c, TypeOfSite__c, Status__c, Launch_Date__c,  GovtRegion__c,Rent__c,TotalSalesPost__c,  
                        (Select MeetingDate__c, RecordType.Name,  TotalSalesPost__c, NPV__c, GrossInvestment__c,NPVGrossInvestment__c, CPAR__c, Tenure__c, Rent__c,SalesAreaPostExcCO__c,ROCE__c  
                            From Financial__r 
                            Where MeetingDate__c <> null and RecordType.Name in ('CCM','CCM Update') 
                            order by MeetingDate__c  desc limit 1), 
                                (Select Risk__c,Launch_Date__c,Launch_FY__c,Launch_Quarter__c, Start_On_Site__c, SDG1__c, SDG2CDG2__c, SDG2R__c, SDG3CDG3__c,aBWS_Licence_Granted__c,aLegalsExchanged__c,  ProjectManager__c, status__c,aObtainPlanningPermission__c 
                                From Investment_Programs__r 
                                where status__c in ('Live','Feasibility','Completed') and Risk__c in('0','25','50','75','100') and Launch_FY__c in ('13/14','14/15')), 
                                InvestmentType__c  
                                From Site__c 
                                	where ID in (Select Site__c 
                                				 from Financial__c 
                                				 	where Recordtype.Name In('CCM','CCM Update'))
                                                    and ID in (Select Site__c from InvestmentProgram__c 
                                                                   where status__c in ('Live','Feasibility','Completed') and Risk__c in('0','25','50','75','100') and Launch_FY__c in ('13/14','14/15')) and Surveyor__c in :surveyor 
                                                                   order by surveyor__c, Launch_Date__c asc limit 999];
         TotalSales = 0;
         averageRoce = 0;
         integer i = 0;  
         double totalNPV = 0;
         double totalGI = 0;
         iBWS = 0;
         iPlanning = 0;
         iLegals = 0;
                                                                
         for(Site__c totals : Records)
         {
             if(totals.Financial__r[0].TotalSalesPost__c != null)
             {
                 TotalSales = TotalSales + totals.Financial__r[0].TotalSalesPost__c;
             }
             if(totals.Financial__r[0].Roce__c != null)
             {
                 averageRoce = averageRoce + totals.Financial__r[0].Roce__c;
                 i++;
             }
             if(totals.Financial__r[0].NPV__c != null && totals.Financial__r[0].GrossInvestment__c != null)
             {
                 totalNPV = TotalNPV + totals.Financial__r[0].NPV__c;
                 totalGI = TotalGI + totals.Financial__r[0].GrossInvestment__c ;
             }
             if(totals.Investment_Programs__r[0].aBWS_Licence_Granted__c != null)
             {
                 iBWS++;
             }
             if(totals.Investment_Programs__r[0].aLegalsExchanged__c != null)
             {
                 iLegals++;
             }
             if(totals.Investment_Programs__r[0].aObtainPlanningPermission__c != null)
             {
                 iPlanning++;
             }
         } 
         averageRoce = averageRoce/i;
         averageNPVGI = (TotalNPV/totalGI)*100;
        return null;
    }
    
    public RAMSuccessTrackerController ()
    {

        surveyor = new List<String>();
    }
    public List<SelectOption> getSurveyors()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        for(User users : [Select id, Name from User where Profile.Name = 'Surveyor' and isActive = true  order by Name])
        {
            options.add(new selectOption(users.Id, users.Name));
        }
        
        return options;
    }
}
This was selected as the best answer