You need to sign in to do that
Don't have an account?
Mark 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!
Thank you again in advance!
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!
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
Let us know if this will help you
All Answers
You need to rewrite your test classes something along following lines..
Hope this helps & if it solves the query then please mark it as Best Answer since it will help other users in the community!
NOTE:- You can also set variable like below
Try to update your code like below
Let us know if this will help you
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.
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
Let us know if this will help you
Gangbusters! Worked beautifully. Thank you all for your help!