You need to sign in to do that
Don't have an account?

Apex test class error: System.NullPointerException: Attempt to de-reference a null object
hey Guys,
I have written an Apex class and Visual Force page which searches the records from custom object. There is a field which is a multi pick list and it is a filter field. User selets the value in that field and all the records meeting the critiriea appear in the 'Results' section. This page has 3 buttons "Search", "Cancel" and "Export to CSV". I am stuck in achieveing desired code coverage. I have written a test class but it's giving me error and code coverage is only 29%. Can someone pleae help me out ? Any help will be appreciated
Here is the error message:
System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.Fetchsiteplacement.FillAggregates: line 77, column 1
Class.TestFetchsiteplacement.myUnitTest: line 49, column 1
Multi picklist filed - Device__c (It can be PC, Tablet, Mobile). Here is Apex class:
public class Fetchsiteplacement{
String devicetype;
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
public List<Site_Placements__c > spRec1{get;set;}
public List<siteplacementwrapper> spwrapper{get;set;}
public List<siteplacementwrapper> spwrapperexcel{get;set;}
public class siteplacementwrapper{
public String Name {get;set;}
}
public Fetchsiteplacement(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
}
public void FillAggregates(){
String devicetype= '';
string[] lststr1=sp.Device_Type__c.split(';');
for (String s1: lststr1) {
devicetype+= '\'' + s1 + '\',';
}
devicetype= devicetype.substring (0,devicetype.length() -1);
spwrapper=new List<siteplacementwrapper>();
spwrapperexcel=new List<siteplacementwrapper>();
String sitePlacementQuery ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec=database.query(sitePlacementQuery);
String sitePlacementQuery1 ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec1=database.query(sitePlacementQuery1);
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records match your criteria'));
}
else {
for(Site_Placements__c sitePlacement:spRec){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapper.add(spr);
}
for(Site_Placements__c sitePlacement:spRec1){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapperexcel.add(spr);
}
}
}
public PageReference FetchExcelReport() {
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
return nextpage;
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://c.cs30.visual.force.com/apex/SiteListPage');
page.SetRedirect(true);
return page;
}}
Apex Test class:
@isTest(seeAllData=true)
private class TestFetchsiteplacement {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Account a = new Account(Name='Test Account');
insert a;
Publisher_Deal_Types__c pubdeal = new Publisher_Deal_Types__c ( Name = 'Test Publisher deal type', Account_Name__c = a.Id);
insert pubdeal;
Site_Placements__c sp = new Site_Placements__c ();
sp.Name = 'Test site placement';
sp.Publisher_Account_Name_clickable__c = a.Id;
sp.Publisher_or_Vendor_Deal_Type_Name__c = pubdeal.Id;
sp.Device_Type__c = 'PC';
insert sp;
PageReference ref = new PageReference('/apex/SiteListPage');
Test.setCurrentPage(ref);
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
Test.setCurrentPage(nextpage);
Test.startTest();
Fetchsiteplacement myController = new Fetchsiteplacement();
myController.FillAggregates();
myController.FetchExcelReport();
myController.CancelSPRec();
Test.stopTest();
Site_Placements__c selectedSP = [SELECT Device_Type__c FROM Site_Placements__c WHERE Id = :sp.Id];
System.assertEquals('PC', selectedSP.Device_Type__c);
}
}
I have written an Apex class and Visual Force page which searches the records from custom object. There is a field which is a multi pick list and it is a filter field. User selets the value in that field and all the records meeting the critiriea appear in the 'Results' section. This page has 3 buttons "Search", "Cancel" and "Export to CSV". I am stuck in achieveing desired code coverage. I have written a test class but it's giving me error and code coverage is only 29%. Can someone pleae help me out ? Any help will be appreciated
Here is the error message:
System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.Fetchsiteplacement.FillAggregates: line 77, column 1
Class.TestFetchsiteplacement.myUnitTest: line 49, column 1
Multi picklist filed - Device__c (It can be PC, Tablet, Mobile). Here is Apex class:
public class Fetchsiteplacement{
String devicetype;
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
public List<Site_Placements__c > spRec1{get;set;}
public List<siteplacementwrapper> spwrapper{get;set;}
public List<siteplacementwrapper> spwrapperexcel{get;set;}
public class siteplacementwrapper{
public String Name {get;set;}
}
public Fetchsiteplacement(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
}
public void FillAggregates(){
String devicetype= '';
string[] lststr1=sp.Device_Type__c.split(';');
for (String s1: lststr1) {
devicetype+= '\'' + s1 + '\',';
}
devicetype= devicetype.substring (0,devicetype.length() -1);
spwrapper=new List<siteplacementwrapper>();
spwrapperexcel=new List<siteplacementwrapper>();
String sitePlacementQuery ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec=database.query(sitePlacementQuery);
String sitePlacementQuery1 ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec1=database.query(sitePlacementQuery1);
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records match your criteria'));
}
else {
for(Site_Placements__c sitePlacement:spRec){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapper.add(spr);
}
for(Site_Placements__c sitePlacement:spRec1){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapperexcel.add(spr);
}
}
}
public PageReference FetchExcelReport() {
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
return nextpage;
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://c.cs30.visual.force.com/apex/SiteListPage');
page.SetRedirect(true);
return page;
}}
Apex Test class:
@isTest(seeAllData=true)
private class TestFetchsiteplacement {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Account a = new Account(Name='Test Account');
insert a;
Publisher_Deal_Types__c pubdeal = new Publisher_Deal_Types__c ( Name = 'Test Publisher deal type', Account_Name__c = a.Id);
insert pubdeal;
Site_Placements__c sp = new Site_Placements__c ();
sp.Name = 'Test site placement';
sp.Publisher_Account_Name_clickable__c = a.Id;
sp.Publisher_or_Vendor_Deal_Type_Name__c = pubdeal.Id;
sp.Device_Type__c = 'PC';
insert sp;
PageReference ref = new PageReference('/apex/SiteListPage');
Test.setCurrentPage(ref);
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
Test.setCurrentPage(nextpage);
Test.startTest();
Fetchsiteplacement myController = new Fetchsiteplacement();
myController.FillAggregates();
myController.FetchExcelReport();
myController.CancelSPRec();
Test.stopTest();
Site_Placements__c selectedSP = [SELECT Device_Type__c FROM Site_Placements__c WHERE Id = :sp.Id];
System.assertEquals('PC', selectedSP.Device_Type__c);
}
}
All that said, try throwing in some system.debug lines in and arround line 77 of your class. use system.debug('tag' + variable name); to output all variables until you find out what's null.
All Answers
All that said, try throwing in some system.debug lines in and arround line 77 of your class. use system.debug('tag' + variable name); to output all variables until you find out what's null.
Your controller creates a new instance of Site_Placements__c but your test never populates that. You probably want to look at controller extentsions for Salesforce [1] and how to test them [2]
Additionally, writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started. I would recommend that you do some reading on testing [3] [4] [5] to get a better understanding. Each of your individual tests should only tests one specific portion of you class (ie a constructor test, fillAggregates test, fetchExcelReport test, etc). You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.salesforce.com/docs/developer/pages/Content/pages_controller_extension.htm
[2] http://www.salesforce.com/docs/developer/pages/Content/pages_controller_error_handling.htm
[3] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[4] http://pcon.github.io/presentations/testing/
[5] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/