• Rama Laxman 5
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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&colon;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>