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
Mark Koch 9Mark Koch 9 

Controller Extension for Task - De-Reference Null Obj & Insufficient Code Coverage

Hi All,

I'm a relative beginner to Apex so bear with me. We're placing an extension on a VF page so that we can run a query to include auto-archived activities but I can't seem to get proper code coverage and have lately been getting an error for de-referencing a null object. Can you take a look at my class and test class and let me know where I went wrong? Your help is incredibly appreciated!
 
public class OFTaskController {

    public OFTaskController(ApexPages.StandardController controller) {

    }

    private final Task tasks;
    public ApexPages.StandardSetController stdCntrlr {get; set;}
    public OFTaskController(ApexPages.StandardSetController controller) {
       stdCntrlr = controller;
        tasks = [SELECT Id, AccountId, Account.Name FROM Task WHERE Id = :ApexPages.currentPage().getParameters().get('id') LIMIT 1 ALL ROWS];
    }
    public Task getTasks() {
        return tasks;
    }
    public PageReference save() {
        update tasks;
        return null;
    }
}
 
@istest
private class OFTaskControllerTest {
    static testMethod void checkOFTask() {
        Date ad = Date.today();
        Task x = new Task();
        x.Subject='Test Task';
        x.Type='Audit';
        x.Status='Completed';
        x.ActivityDate=ad;
        x.Priority='Normal';
        x.Whatid='0014600000jvg5Y';
        insert x;
        
        test.startTest();
        Test.setCurrentPage(Page.OFTask_Test);
          ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
          //stdSetController.setSelected(x);
          OFTaskController ext = new OFTaskController(stdSetController);
        ext.getTasks();
        //ext.save();
        test.stopTest();
        
        }
}

Thank you again in advance!
Best Answer chosen by Mark Koch 9
Amit Chaudhary 8Amit Chaudhary 8
Issue is coming because in your test class you are useing below line (StandardController)
ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
But you need (StandardSetController)
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount); stdSetController.setSelected(lstAccount); OFTaskController ext = new OFTaskController(stdSetController);

Please check below post for test class example
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html


Please try below test class
@istest
private class OFTaskControllerTest 
{
    static testMethod void checkOFTask() 
	{

		List <Account> lstAccount = new List<Account>();
 
        Account act = New Account();
        act.Name = 'Test Account';
        Id rtid = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business Account - Suspect / Prospect').getRecordTypeId();
        act.RecordTypeId = rtid;
        act.Type = 'Suspect';
		lstAccount.add(act);
		insert  lstAccount;
 
        Date ad = Date.today();
        Task x = new Task();
        x.Subject='Test Task';
        x.Type='Audit';
        x.Status='Completed';
        x.ActivityDate=ad;
        x.Priority='Normal';
        x.Whatid= lstAccount[0].Id;
        insert x;
        
        test.startTest();
			PageReference pageRef = Page.OFTask_Test;
			pageRef.getParameters().put('id',x.id);
			Test.setCurrentPage(pageRef);
			ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount);
			stdSetController.setSelected(lstAccount);
			OFTaskController ext = new OFTaskController(stdSetController);

			ext.getTasks();
			ext.save();
        test.stopTest();
        
    }
}

Let us know if this will help you

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
On line number 11 when you say x.WhatId = '0014600000jvg5Y'; .... it would not know the record because record has not been inserted yet.

You need to rewrite your test classes something along following lines..
 
@istest
private class OFTaskControllerTest {
    static testMethod void checkOFTask() {

        Account act = New Account();
        act.Name = 'Test Account';
        // Insert all the required field here.
        insert act;

        Date ad = Date.today();
        Task x = new Task();
        x.Subject='Test Task';
        x.Type='Audit';
        x.Status='Completed';
        x.ActivityDate=ad;
        x.Priority='Normal';
        x.Whatid= act.Id;
        insert x;
        
        test.startTest();
        Test.setCurrentPage(Page.OFTask_Test);
          ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
          //stdSetController.setSelected(x);
          OFTaskController ext = new OFTaskController(stdSetController);
        ext.getTasks();
        //ext.save();
        test.stopTest();
        
        }
}

Hope this helps & if it solves the query then please mark it as Best Answer since it will help other users in the community!
Mark Koch 9Mark Koch 9
I'm up to this now but still de-referencing a null object...
@istest
private class OFTaskControllerTest {
    static testMethod void checkOFTask() {

        Account act = New Account();
        act.Name = 'Test Account';
        Id rtid = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business Account - Suspect / Prospect').getRecordTypeId();
        act.RecordTypeId = rtid;
        act.Type = 'Suspect';
        insert act;

        Date ad = Date.today();
        Task x = new Task();
        x.Subject='Test Task';
        x.Type='Audit';
        x.Status='Completed';
        x.ActivityDate=ad;
        x.Priority='Normal';
        x.Whatid= act.Id;
        insert x;
        
        test.startTest();
        PageReference pageRef = Page.OFTask_Test;
        pageRef.getParameters().put('Id=',x.id);
        Test.setCurrentPage(pageRef);
          ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
          //stdSetController.setSelected(x);
          OFTaskController ext = new OFTaskController(stdSetController);
        ext.getTasks();
        ext.save();
        test.stopTest();
        
        }
}
Glyn Anderson 3Glyn Anderson 3
On line 24 of your test class, change: "put('Id=',x.id)" to "put('id',x.id)".  Your test is adding a parameter called 'Id=', but the extension is looking for a parameter called 'id'.  Those aren't the same, so the extension is getting null for the value of the id parameter.
Amit Chaudhary 8Amit Chaudhary 8
Issue coming because of below line
pageRef.getParameters().put('Id=',x.id);
You need to update like below
pageRef.getParameters().put('id',x.id);

NOTE:- You can also set variable like below
test.startTest();

			ApexPages.currentPage().getParameters().put('id',x.id);	
			ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
			OFTaskController ext = new OFTaskController(stdSetController);
			ext.getTasks();
			ext.save();
        test.stopTest();




Try to update your code like below
@istest
private class OFTaskControllerTest 
{
    static testMethod void checkOFTask() 
	{

        Account act = New Account();
        act.Name = 'Test Account';
        Id rtid = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business Account - Suspect / Prospect').getRecordTypeId();
        act.RecordTypeId = rtid;
        act.Type = 'Suspect';
        insert act;

        Date ad = Date.today();
        Task x = new Task();
        x.Subject='Test Task';
        x.Type='Audit';
        x.Status='Completed';
        x.ActivityDate=ad;
        x.Priority='Normal';
        x.Whatid= act.Id;
        insert x;
        
        test.startTest();
        PageReference pageRef = Page.OFTask_Test;
        pageRef.getParameters().put('id',x.id);
        Test.setCurrentPage(pageRef);
		
		ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
		//stdSetController.setSelected(x);
		OFTaskController ext = new OFTaskController(stdSetController);
        ext.getTasks();
        ext.save();
        test.stopTest();
        
    }
}

Let us know if this will help you
Mark Koch 9Mark Koch 9
Tried with the new code.... still getting the following:


System.NullPointerException: Attempt to de-reference a null object
Class.OFTaskController.save: line 17, column 1 Class.OFTaskControllerTest.checkOFTask: line 33, column 1

I appreciate you folks pointing out the parameter error. That slipped by me.
Amit Chaudhary 8Amit Chaudhary 8
Issue is coming because in your test class you are useing below line (StandardController)
ApexPages.StandardController stdSetController = new ApexPages.StandardController(x);
But you need (StandardSetController)
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount); stdSetController.setSelected(lstAccount); OFTaskController ext = new OFTaskController(stdSetController);

Please check below post for test class example
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html


Please try below test class
@istest
private class OFTaskControllerTest 
{
    static testMethod void checkOFTask() 
	{

		List <Account> lstAccount = new List<Account>();
 
        Account act = New Account();
        act.Name = 'Test Account';
        Id rtid = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business Account - Suspect / Prospect').getRecordTypeId();
        act.RecordTypeId = rtid;
        act.Type = 'Suspect';
		lstAccount.add(act);
		insert  lstAccount;
 
        Date ad = Date.today();
        Task x = new Task();
        x.Subject='Test Task';
        x.Type='Audit';
        x.Status='Completed';
        x.ActivityDate=ad;
        x.Priority='Normal';
        x.Whatid= lstAccount[0].Id;
        insert x;
        
        test.startTest();
			PageReference pageRef = Page.OFTask_Test;
			pageRef.getParameters().put('id',x.id);
			Test.setCurrentPage(pageRef);
			ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount);
			stdSetController.setSelected(lstAccount);
			OFTaskController ext = new OFTaskController(stdSetController);

			ext.getTasks();
			ext.save();
        test.stopTest();
        
    }
}

Let us know if this will help you
This was selected as the best answer
Mark Koch 9Mark Koch 9

Gangbusters! Worked beautifully. Thank you all for your help!