• San India
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
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