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
SimonJaiSimonJai 

Testing for Custom Controller

Hi there,

 

Wrote a custom controller to display an overall view of all our services, but the test method is returning a "System.NullPointerException: Attempt to de-reference a null object" on this line:

 

@IsTest(SeeAllData=true)
public static void testServiceTimeline() {				
		
	ServiceTimeline controller = new ServiceTimeline();		
		
}

 

This is my controller:

 

public class ServiceTimeline {
	
	public List<ServiceTime> serviceList { get; set; }
	
	public ServiceTimeline() {
		
		List<Service__c> ListOfServices= [
			SELECT 
				Name, 
				RecordType.Name,
				Address_Single_Line__c,
				Status__c,
				CreatedDate,
				Stage_Prospect__c, 
				Stage_Site_Survey__c, 
				Stage_Scheduling_Install__c, 
				Stage_Provisioning__c, 
				Stage_Pending_Billing__c, 
				Stage_Order_Received__c, 
				Stage_Order_Completion__c, 
				Stage_Obtaining_Install_Approval__c, 
				Stage_Awaiting_Install__c,
				Stage_Design_Request__c
			FROM 
				Service__c 
			WHERE 
				Status__c NOT IN ('Order Completion', 'Cancelled', 'Stalled', 'Unserviceable')
				AND RecordType.Name NOT IN ('Prospect','Ethernet Interconnect')
			];
		
		if (ListOfServices.size() > 0) {
			for (Service__c tempService : ListOfServices) {
				serviceList.add(new ServiceTime(tempService));
			}
		} else {
			Util.addERRORMessage('No Services are currently being installed.');
		}
		
	}
}

 

Any help would be appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
ibtesamibtesam
public class ServiceTimeline {
	
	public List<ServiceTime> serviceList { get; set; }
	
	public ServiceTimeline() {
		serviceList = new List<ServiceTime>();
		List<Service__c> ListOfServices = new List<Service__c>();
ListOfServices= [ SELECT Name, RecordType.Name, Address_Single_Line__c, Status__c, CreatedDate, Stage_Prospect__c, Stage_Site_Survey__c, Stage_Scheduling_Install__c, Stage_Provisioning__c, Stage_Pending_Billing__c, Stage_Order_Received__c, Stage_Order_Completion__c, Stage_Obtaining_Install_Approval__c, Stage_Awaiting_Install__c, Stage_Design_Request__c FROM Service__c WHERE Status__c NOT IN ('Order Completion', 'Cancelled', 'Stalled', 'Unserviceable') AND RecordType.Name NOT IN ('Prospect','Ethernet Interconnect') ]; if (ListOfServices.size() > 0) { for (Service__c tempService : ListOfServices) { serviceList.add(new ServiceTime(tempService)); } } else { Util.addERRORMessage('No Services are currently being installed.'); } } }

All Answers

ibtesamibtesam
public class ServiceTimeline {
	
	public List<ServiceTime> serviceList { get; set; }
	
	public ServiceTimeline() {
		serviceList = new List<ServiceTime>();
		List<Service__c> ListOfServices = new List<Service__c>();
ListOfServices= [ SELECT Name, RecordType.Name, Address_Single_Line__c, Status__c, CreatedDate, Stage_Prospect__c, Stage_Site_Survey__c, Stage_Scheduling_Install__c, Stage_Provisioning__c, Stage_Pending_Billing__c, Stage_Order_Received__c, Stage_Order_Completion__c, Stage_Obtaining_Install_Approval__c, Stage_Awaiting_Install__c, Stage_Design_Request__c FROM Service__c WHERE Status__c NOT IN ('Order Completion', 'Cancelled', 'Stalled', 'Unserviceable') AND RecordType.Name NOT IN ('Prospect','Ethernet Interconnect') ]; if (ListOfServices.size() > 0) { for (Service__c tempService : ListOfServices) { serviceList.add(new ServiceTime(tempService)); } } else { Util.addERRORMessage('No Services are currently being installed.'); } } }
This was selected as the best answer
SimonJaiSimonJai
Forgot to initialise the variables as new lists.

Cheers!