function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler McCartyTyler McCarty 

Account Type Dropdown search box

Hello, Im getting this error when trying to populate a dropdown box with the account types.

System.NullPointerException: Argument 1 cannot be null  Class.AccountTypeController.getAccounts: line 13, column 1

Apparently 'Type' is a reserved word in apex but is also the API name of the Account field, Type. It won't return the field because of this. Any way around this besides changing the API name?

Apex code:
public class AccountTypeController 
{
    public string Account{get; set;}
    public String sortOrder = 'Type';
    
    
    
    public List<SelectOption> getAccounts()
    {
        List<SelectOption> AccountList = new List<SelectOption>();
        for(Account act: [SELECT Name, Type, Website, BillingAddress
                                              FROM Account])
            AccountList.add(new SelectOption(act.Type, act.Type));

        return AccountList;
      
    }
    
   
    
    public void sortByType()
    {
        this.sortOrder = 'Type';
    }

}

vf code:
<apex:page controller="AccountTypeController">
  <apex:form >
    <apex:pageBlock title="Accounts">
        <apex:pageBlockSection >
        
            <apex:selectList value="{!Account}" size="1">
                 <apex:selectOption itemLabel="-Select Account Type-" itemValue="-Select-"/>                     
                  <apex:selectOptions value="{!Accounts}"/>
                  <apex:actionSupport event="onchange" reRender="pb" status="actStatusId"> </apex:actionSupport>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>  
  </apex:form>
</apex:page>
 
Ajay K DubediAjay K Dubedi
Hi Tyler,
Try the below code now it works fine:

vf code:
<apex:page controller="AccountTypeController">
  <apex:form >
    <apex:pageBlock title="Accounts">
        <apex:pageBlockSection >
            <apex:selectList value="{!Account}" size="1">
                 <apex:selectOption itemLabel="-Select Account Type-" itemValue="-Select-"/>                     
                  <apex:selectOptions value="{!Accounts}"/>
                  <apex:actionSupport event="onchange" reRender="pb" status="actStatusId"> </apex:actionSupport>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>  
  </apex:form>
</apex:page>

controller code:
public class AccountTypeController {
    public string Account{get; set;}
    public String sortOrder = 'Type';
    public List<SelectOption> getAccounts(){
        List<SelectOption> AccountList = new List<SelectOption>();
        for(Account act: [SELECT Name, Type, Website, BillingAddress FROM Account]){
            if(act.Type != null){
                AccountList.add(new SelectOption(act.Type, act.Type));
            }
        }
        return AccountList;
    }
    public void sortByType()
    {
        this.sortOrder = 'Type';
    }
    
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi