+ Start a Discussion
San IndiaSan India 

Multiple Searches needed in VF Page

Hi All,

I have a requirement like
1st need to search all accounts with matching text from textbox and result need to show in dropdown in VF Page and (this is done)
again from dropdown any account selected need to search that account contacts ans show as table in same VF page (not done getting error) like

Visualforce Error
Help for this Page
System.NullPointerException: Attempt to de-reference a null object
Class.accountListDropdown.getacctContact: line 41, column 1

here is my code VF Page

<apex:page controller="accountListDropdown" >
  <apex:form >
  
  <apex:outputlabel >Enter Account Name : </apex:outputlabel>
  <apex:inputtext value="{!accountname}" />
  <apex:commandButton action="{!getSearchedAccts}" value="Go.."/>
  
  <apex:pageBlock >
      <apex:pageblockTable value="{!actList}" var="act" rendered="{!actList != null}">
          <apex:column value="{!act.Id}"/>
          <apex:column value="{!act.Name}"/>
      </apex:pageblockTable>
  
    <apex:pageblocksection >
        <apex:outputlabel >Select Account Name to get Contacts: </apex:outputlabel> 
        <apex:selectList id="actOptions" value="{!actOptions}" size="1" rendered="{!actList != null}">
         <apex:actionSupport event="onchange" action="{!accounContacts}" />
            <apex:selectOptions value="{!items}"/>
        </apex:selectList>  
    </apex:pageblocksection>
    
     <apex:pageblockTable value="{!acctContact}" var="cont" rendered="{!actList != null} && {!acctContact != null}">
      <apex:column value="{!cont.Id}"/>
      <apex:column value="{!cont.Name}"/>
    </apex:pageblockTable>
      
  </apex:pageBlock>
  </apex:form>
</apex:page>

My Controller

public class accountListDropdown { 
    public String accountname { get; set; }    
    public list<Account> actList { get; set; }  
    public list<Contact> contactList { get; set; }   
    public String actOptions { get; set; }
    public accountListDropdown(){

    }     
    public pageReference getSearchedAccts() {
        actList = new list<Account>();
        //system.debug('SELECT Id,Name FROM Account WHERE Name Like' +'%'+ accountname +'%');
        string tempInput = '%' + accountname  + '%';
        actList = [SELECT Id,Name FROM Account WHERE Name Like : tempInput];     
        return null;
    }
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();       
        if(!actList.isEmpty()){
            for(Account c : actList){
                options.add(new SelectOption(c.Id, c.Name));
            }
        }             
        return options;
    }   
    public PageReference accounContacts() {
        contactList = new list<Contact>();
        
        
        contactList = [SELECT  Id,Name FROM Contact WHERE AccountId =  : actOptions];   
        system.debug(contactList); 
        return null;
    }
    public List<Contact> getacctContact() {
    
        contactList = new list<Contact>();
        
        
        contactList = [SELECT  Id,Name FROM Contact WHERE AccountId =  : actOptions];   
    
        List<Contact> actContatsData = new List<Contact>();         
        if(!actList.isEmpty() && !contactList.isEmpty()){
            for(Contact c : contactList){
                actContatsData.add(c);
            }
        }             
        return actContatsData;
    }
}
 please any one help on this
hitesh90hitesh90
Hello San,

Try to use following controller code to overcome this exception.

Apex Class:
public class accountListDropdown { 
    public String accountname { get; set; }    
    public list<Account> actList { get; set; }  
    public list<Contact> contactList { get; set; }   
    public String actOptions { get; set; }
    public accountListDropdown(){

    }     
    public pageReference getSearchedAccts() {
        actList = new list<Account>();
        //system.debug('SELECT Id,Name FROM Account WHERE Name Like' +'%'+ accountname +'%');
        string tempInput = '%' + accountname  + '%';
        actList = [SELECT Id,Name FROM Account WHERE Name Like : tempInput];     
        return null;
    }
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();       
        if(!actList.isEmpty()){
            for(Account c : actList){
                options.add(new SelectOption(c.Id, c.Name));
            }
        }             
        return options;
    }   
    public PageReference accounContacts() {
        contactList = new list<Contact>();
        
        
        contactList = [SELECT  Id,Name FROM Contact WHERE AccountId =  : actOptions];   
        system.debug(contactList); 
        return null;
    }
    public List<Contact> getacctContact() {
    
        contactList = new list<Contact>();
        
        
        contactList = [SELECT  Id,Name FROM Contact WHERE AccountId =  : actOptions];   
    
        List<Contact> actContatsData = new List<Contact>();         
        if(actList != null && !actList.isEmpty() && !contactList.isEmpty()){
            for(Contact c : contactList){
                actContatsData.add(c);
            }
        }             
        return actContatsData;
    }
}



Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
San IndiaSan India
Hi Hitesh 

Thank you for the reply , this solution got worked bit, means vf page load now error is not showing but when i enter some text in textbox and press command button 'Go' for account search , again this error getting

Visualforce Error
Help for this Page
System.NullPointerException: Attempt to de-reference a null object
Class.accountListDropdown.getacctContact: line 39, column 1


To make sure , here i am calling controller twice 1 time for text matching accounts search and 2nd time for selected account contacts data

please check this issue
hitesh90hitesh90
Hi San,

Modify below line in your code.

Previous code:
if(actList != null && !actList.isEmpty() && !contactList.isEmpty()){

New Code:
if(actList != null && contactList!= null && !actList.isEmpty() && !contactList.isEmpty()){
Let me know if you have any question on this.
Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/