You need to sign in to do that
Don't have an account?
Test Method - System.NullPointerException: Attempt to de-reference a null object
I'm getting this message on my test method for a custom lookup. It's also commented in the code above line 56.
It appears to me that I'm using the same concept a the call to save Contact that works. The code test out fine interactively.
Any clues? Thanks.
THE TEST METHOD IS:
/* Purpose : This class contains unit tests for the Custom Lookup.
*/
@isTest(seeAllData=true)
private class test_CustomContactLkpCntl {
public static testMethod void test_CustomContactLkpCntl() {
//Use the PageReference Apex class to instantiate a page
PageReference pageRef = Page.CustomContactLookup;
//In this case, the Visualforce page named Custom...Lookup is the starting point of this test method.
Test.setCurrentPage(pageRef);
//Instantiate and construct the controller class.
customContactLookupController controller = new customContactLookupController();
//Instantiate a new controller with all parameters in the page
controller = new customContactLookupController();
contact c = new Contact();
//calling an Action method. Same as calling any other Apex method.
//Normally this is executed by a user clicking a button or a link from the Visualforce
//page, but in the test method, just test the action method the same as any
//other method by calling it directly.
//get parameters to page URL
ApexPages.currentPage().getParameters();
//The .getURL will return the page url the Search method returns.
controller.searchString = null;
// String srchPage = controller.search().getUrl();
// System.assertEquals('/apex/customcontactlookup', srchPage);
//Example of calling the 'setter' method for several properties.
//but in a test method, just call the setter method directly.
controller.Contact.LastName = 'Test Last Name';
controller.Contact.FirstName = 'Test First Name';
String newPage = controller.saveContact().getUrl();
//Verify that the success page displays
System.assertEquals('/apex/customcontactlookup', newPage);
controller.contact.Description = 'Test Desc';
String editPage = controller.saveContact().getUrl();
//Verify that the success page displays
System.assertEquals('/apex/customcontactlookup', editPage);
// Next line ERROR - System.NullPointerException: Attempt to de-reference a null object
String srchPage = controller.search().getUrl();
System.assertEquals('/apex/customcontactlookup', srchPage);
}
}
THE CONTROLLER IS:
public with sharing class CustomContactLookupController {
public CustomContactLookupController(ApexPages.StandardController controller) {
}
public Contact contact {get;set;} // new account to create
public List<Contact> results{get;set;} // search results
public string searchString{get;set;} // search keyword
public Boolean IsNew { get; set; }
public String NewTitle { get; set; }
public CustomContactLookupController() {
contact = new Contact();
IsNew = true;
NewTitle = 'Add';
results = new List<Contact>();
// get the current search string
// chgd to below searchString = System.currentPageReference().getParameters().get('lksrch');
searchString = '';
runSearch();
}
// performs the keyword search
public PageReference search() {
runSearch();
return null;
}
// prepare the query and issue the search command
private void runSearch() {
// TODO prepare query string for complex serarches & prevent injections
results = new List<Contact>();
results = performSearch(searchString);
}
// run the search and return the records found.
private List<Contact> performSearch(string searchString) {
String soql = 'select id, Name, Account.Id, Account.Name, Title, Email from contact';
if(searchString != '' && searchString != null)
soql = soql + ' where name LIKE \'%' + searchString +'%\'';
soql = soql + ' limit 25'; // 100';
System.debug(soql);
return database.query(soql);
}
// save the new contact record
public PageReference saveContact() {
if (IsNew == true)
{
insert contact;
IsNew = false;
NewTitle = '* EDIT New Contact *';
}
else
{
upsert contact;
}
searchString = Contact.FirstName = ' ' + Contact.LastName;
runSearch();
return Page.CustomContactLookup;
}
// used by the visualforce page to send the link to the right dom element
public string getFormTag() {
return System.currentPageReference().getParameters().get('frm');
}
// used by the visualforce page to send the link to the right dom element for the text box
public string getTextBox() {
return System.currentPageReference().getParameters().get('txt');
}
}
You have this code in your controller:
And then this in your test method:
So I think that the compiler is trying to run this:
And that may be why you are getting the null pointer exception.
You may want to consider:
hi try this code
@isTest
public class testcustomcontact
{
static testmethod void Test_CustomContactLookupController()
{
Account testAccount =new Account();
testAccount.name='xxx';
insert testAccount;
Contact testContact =new Contact();
testContact.lastname='xxxx';
testContact.accountid=testAccountid;
insert testContact;
ApexPages.StandardController controller = new ApexPages.StandardController(New Contact());
CustomContactLookupController ccl=new CustomContactLookupController(controller );
ccl.search();
ccl.saveContact();
ccl.getFormTag();
ccl.getTextBox();
}
}