0

I have created a simple VF search page which returns a list of email addresses but getting NULL pointer exception in line 14 when I execute the search in VF page.

Seeing that line 14 is the line which has NULL, I added a debug before that which return the ID in a.Merchant_Profile__r.Profile_Account_Name__c field. Is there a reason why it's NULL when trying to add this field into the Set acc?

Line 13: system.debug(a.Merchant_Profile__r.Profile_Account_Name__c); Line 14: acc.add(a.Merchant_Profile__r.Profile_Account_Name__c);

Cheers, Chris

**My controller class:**

public with sharing class OutageContacts {
  public list <contact> con {get;set;}
  public Set<ID> acc {get;set;}
  public string searchstring {get;set;}
  public string contactlist {get;set;}
  public OutageContacts(ApexPages.StandardController controller) {
  }  
  public void search(){
    searchstring = '%'+searchstring+'%';
    system.debug(searchstring);
    for(Acquirer_Link__c a : [SELECT Merchant_Profile__r.Profile_Account_Name__c, Merchant_Profile__r.Merchant_ID__c FROM Acquirer_Link__c WHERE Acquirer_Name__c LIKE :searchstring])
    {
      system.debug(a.Merchant_Profile__r.Profile_Account_Name__c);
      acc.add(a.Merchant_Profile__r.Profile_Account_Name__c);
    }

    string contactlist='SELECT Account.name, Account.Line_Of_Business__c, email FROM Contact WHERE AccountID IN :acc';  
    con = Database.query(contactlist);

  }
  public void clear(){
    con.clear();  
  }  

}


**The VF page:**

<apex:page standardController="contact" extensions="OutageContacts " sidebar="false"> 
  <apex:form >  
    <apex:inputText value="{!searchstring}" label="Input"/>   
    <apex:commandButton value="Search records" action="{!search}"/>  
    <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
     <apex:pageblockTable value="{!con}" var="c">  
     <apex:column >  
        <apex:outputlink value="https://test.salesforce.com/{!c.id}">{!c.Name}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!c.id}"/>  
     </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
</apex:page>

2 Answers 2

3

You never initialized acc, so you need to do that before you attempt to access it:

public void search(){
    searchstring = '%'+searchstring+'%';
    acc = new Set<Id>();
    system.debug(searchstring);
    for(Acquirer_Link__c a : [SELECT Merchant_Profile__r.Profile_Account_Name__c, Merchant_Profile__r.Merchant_ID__c FROM Acquirer_Link__c WHERE Acquirer_Name__c LIKE :searchstring])
    {
      system.debug(a.Merchant_Profile__r.Profile_Account_Name__c);
      acc.add(a.Merchant_Profile__r.Profile_Account_Name__c);
    }

    string contactlist='SELECT Account.name, Account.Line_Of_Business__c, email FROM Contact WHERE AccountID IN :acc';  
    con = Database.query(contactlist);

  }

Also, keep in mind that if clear is called before search, you'll also get a null pointer exception. You should probably initialize con in your constructor:

public OutageContacts(ApexPages.StandardController controller) {
    con = new Contact[0];
}

Alternatively, you could also choose to construct a new list instead of clearing the existing list:

public void clear() {
    con = new Contact[0];
}
1
  • than you for the input, managed to get it work now
    – user16806
    Oct 22, 2015 at 2:18
0

Updated the Apex class to:

public with sharing class OutageContacts {
  public list <contact> con {get;set;}
  public Set<ID> acc {get;set;}
  public string searchstring {get;set;}
  public string contactlist {get;set;}
  public OutageContacts(ApexPages.StandardController controller) {
  }  
  public void search(){
    acc = new Set<Id>();
    con = new List<contact>();
    system.debug(searchstring);
    for(Acquirer_Link__c a : [SELECT Id,Merchant_Profile__r.Profile_Account_Name__c, Merchant_Profile__r.Merchant_ID__c FROM Acquirer_Link__c WHERE Acquirer_Name__c LIKE :searchstring])
    {
      system.debug(a.Merchant_Profile__r.Profile_Account_Name__c);
      acc.add(a.Merchant_Profile__r.Profile_Account_Name__c);
    }
    con = [SELECT Id, Account.name, Account.Line_Of_Business__c, name, email FROM Contact WHERE AccountID IN :acc];
  }
  public void clear(){
    con.clear();
    searchstring = '';
  }
  public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('%CBA%', 'CBA'));
    options.add(new SelectOption('%ANZ%', 'ANZ'));
    options.add(new SelectOption('%MAA%', 'MAA'));
    options.add(new SelectOption('%WBC%', 'WBC'));
    return options;   
  }
}

Updated the Apex Page to:

<apex:page standardController="contact" extensions="OutageContacts" sidebar="false"> 
  <apex:form >
    <p>
    <span>Acquirer Link: </span>
    <apex:selectList value="{!searchstring}" multiselect="false" size="1">
      <apex:selectOptions value="{!items}"/>
    </apex:selectList>
    <br/><br/>
    <apex:commandButton value="Search records" action="{!search}"/>  
    <apex:commandButton value="Clear records" action="{!clear}"/>
    </p>  
   <apex:pageBlock title="Search Result" mode="maindetail">  
     <apex:pageblockTable value="{!con}" var="c">  
     <apex:column >  
        <apex:outputlink value="https://test.salesforce.com/{!c.id}">{!c.Account.name}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!c.Account.Line_Of_Business__c}"/>  
     <apex:column value="{!c.name}"/>
     <apex:column value="{!c.email}"/>
     </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>
  DEBUG data<br/>
  You have Selected:<br/>
  Acquirer: {!searchstring}<br/>
  List of Account IDs returned: {!acc}<br/>
  List of Contacts IDs returned: {!con}<br/>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .