You need to sign in to do that
Don't have an account?
System.NullPointerException: Attempt to de-reference a null objectStack in test class
Hi All
I am trying to write a test class for this apex class and VF page but im getting this error
System.NullPointerException: Attempt to de-reference a null objectStack TraceClass.Custom_Account_Lookup_Search.saveAccount: line 42, column 1
Class.Test_Custom_Account_Lookup_Search.test1: line 24, column 1
how to solve this issue;
Test Class:
@isTest(Seealldata = true)
public class Test_Custom_Account_Lookup_Search
{
Static testmethod void test1()
{
PageReference pg = page.Custom_Account_Lookup_Search_page;
Test.setcurrentPage(pg);
//account a = new Account( name='foo' ,BillingStreet ='west',BillingCity ='test') ;
//insert a;
Account acc = new Account();
acc.Name='voltum';
acc.BillingStreet='Santhom High Road';
acc.BillingCity='Chennai';
acc.BillingState='TamilNadu';
acc.BillingCountry='India';
//acc.BillingZipPostal='6000028';
acc.Email__c='perisoft@voltum.com';
acc.Fax='+49 9134 85 28732';
acc.Website='http://www.voltum.com.au/';
insert acc;
Site__c site = new Site__c();
ApexPages.StandardController stdController1 = new ApexPages.StandardController(site);
Custom_Account_Lookup_Search thecontroller1 = new Custom_Account_Lookup_Search(stdController1);
thecontroller1.saveAccount();
}
}
Aex class:
public with sharing class Custom_Account_Lookup_Search {
public Account account {get;set;} // new account to create
public List<Account> results{get;set;} // search results
public string searchString{get;set;} // search keyword
public Custom_Account_Lookup_Search() {
account = new Account();
// get the current search string
searchString = System.currentPageReference().getParameters().get('lksrch');
runSearch();
}
// performs the keyword search
public PageReference search() {
runSearch();
return null;
}
public Custom_Account_Lookup_Search(ApexPages.StandardController stdController)
{
}
// prepare the query and issue the search command
private void runSearch() {
// TODO prepare query string for complex serarches & prevent injections
results = performSearch(searchString);
}
// run the search and return the records found.
private List<Account> performSearch(string searchString) {
String soql = 'select id, name from account';
if(searchString != '' && searchString != null)
soql = soql + ' where name LIKE \'%' + searchString +'%\'';
soql = soql + ' limit 25';
System.debug(soql);
return database.query(soql);
}
// save the new account record
public PageReference saveAccount() {
insert account;
// reset the account
account = new Account();
return null;
}
// 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');
}
}
VF Page:
<apex:page controller="Custom_Account_Lookup_Search"
title="Search"
showHeader="false"
sideBar="false"
tabStyle="Account"
id="pg">
<apex:form >
<apex:outputPanel id="page" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
<apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">
<!-- SEARCH TAB -->
<apex:tab label="Search" name="tab1" id="tabOne">
<apex:actionRegion >
<apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
<apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
<apex:inputText id="txtSearch" value="{!searchString}" />
<span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
</apex:outputPanel>
<apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
<apex:pageBlock id="searchResults">
<apex:pageBlockTable value="{!results}" var="a" id="tblResults">
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Name</apex:outputPanel>
</apex:facet>
<apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:actionRegion>
</apex:tab>
<!-- NEW ACCOUNT TAB -->
<apex:tab label="New Account" name="tab2" id="tabTwo">
<apex:pageBlock id="newAccount" title="New Account" >
<apex:pageBlockButtons >
<apex:commandButton action="{!saveAccount}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlockSection columns="2">
<apex:repeat value="{!$ObjectType.Account.FieldSets.CustomAccountLookup}" var="f">
<apex:inputField value="{!Account[f]}"/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:tab>
</apex:tabPanel>
</apex:outputPanel>
</apex:form>
</apex:page>
As mentioned you will need to pass the required values to the account variable before it is inserted into the DB.
I would expect that in a normal transaction, your VF page makes sure that the field values are populated, but in the testmethod, we need to explicityly provide these values.
The following code needs to be updated.
to something like
All Answers
Is the functionality working..?
account variable you are doing get set in your class but it is not coming from VF Page ..
thanks for reply Neha Lund the VF Page and Apex class is working fine when i run test class its getting this error
System.NullPointerException: Attempt to de-reference a null objectStack TraceClass.Custom_Account_Lookup_Search.saveAccount: line 42, column 1
Class.Test_Custom_Account_Lookup_Search.test1: line 24, column 1
Please mark this as a solution if this fixes the issue so that other find this useful.
thanks for reply vbs can u please give the extact code
Instead of this in your test class:
Try this:
This will call the default constructor where the account is initialised correctly. Although your test class lacks a lot of testing ;-)
now am getting this error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Class.Custom_Account_Lookup_Search.saveAccount: line 42, column 1
Class.Test_Custom_Account_Lookup_Search.test1: line 29, column 1
account.Name = <Account Name> OR
Account account = new Account(Name = <Account Name>)
Although I foresee many more fields being required so all those will need to be included as well.
@isTest(Seealldata = true)
public class Test_Custom_Account_Lookup_Search
{
Static testmethod void test1()
{
PageReference pg = page.Custom_Account_Lookup_Search;
Test.setcurrentPage(pg);
Custom_Account_Lookup_Search thecontroller1 = new Custom_Account_Lookup_Search();
thecontroller1.saveAccount();
Account acc = new Account(Name = 'TestAccountName');
insert acc;
}
}
Now i tried this but getting the same error
As mentioned you will need to pass the required values to the account variable before it is inserted into the DB.
I would expect that in a normal transaction, your VF page makes sure that the field values are populated, but in the testmethod, we need to explicityly provide these values.
The following code needs to be updated.
to something like
thank u very much Jerun Jose
public static String testMethod(){
Decimal Maxnumber = 0;
if(Maxnumber > 500){
}
}
return string;
}
need to write a test class for the above class