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
divya manohardivya manohar 

navigate list view error

Hello
I have created visual force page to navigate the account list view.
when I execute I get error"attempt to deference a null object"
pls le tme know eeher I am going wrong.
public class ListViewDemo
{

   string basequery='Select ID, Name FROM Account ORDER BY NAME ASC';
  
   private Integer pageSize = 10;
   
   public String AccFilterId {get; set;}

   public ListViewDemo(){}


   public ApexPages.StandardSetController AccSetController  //this is a property
   {
     get
        {
          if (AccSetController == null)
          {
            ApexPages.StandardSetController AccSetController=new ApexPages.StandardSetController(Database.getQueryLocator(basequery));
     
            AccSetController.setPageSize(pageSize);

            if (AccFilterId != null)
            {
              AccSetController.setFilterID(AccFilterId);
            }
          }
           
          return AccSetController;
        }
     set;
   }
   
   public ListViewDemo(ApexPages.StandardSetController c) {   }


   //Navigate to first Page
    public void firstPage()
    {
      AccSetController.first();

    }

    //Navigate to last Page
    public void lastPage()
    {
      AccSetController.last();
    }


    //Navigate to Next page

    public void next()
    {
      if(AccSetController.getHasNext())
      {
        AccSetController.next();
      }
    }


    //Navigate to Prev Page
    public void prev()
    {
      if(AccSetController.getHasPrevious())
      {
        AccSetController.previous();
      }
    }
    
    public SelectOption[] getAccountExistingViews()
    {
     return AccSetController.getListViewOptions();
    }
    
    
    public List<Account> getAccounts()
    {
        return AccSetController.getRecords();
    }

    
    public PageReference resetFilter()
    {
        AccSetController = null;
        
        AccSetController.setPageNumber(1);
        
        return null;
    }
}

<apex:page controller="ListViewDemo">
  
  <apex:form id="pageForm">
  
  <apex:selectList value="{!AccFilterId}"
                   size="1"
                   id="filterMenu">
                   
  <apex:selectOptions value="{!AccountExistingViews}"/>
  <apex:actionSupport event="onchange"  
                      action="{!resetFilter}" 
                      rerender="AccntTable" 
                      status="ajaxStatus"/>
  </apex:selectList>
  
  <apex:actionStatus id="ajaxStatus" 
                     startText="Loading..."  
                     stopText=""/>
  
   <apex:pageBlock title="Accounts">
   
    <apex:pageBlockButtons>

                <apex:commandButton action="{!firstPage}" 
                                    value="|<<" reRender="AccntTable"  
                                    status="ajaxStatus"/>

                <apex:commandButton action="{!prev}" 
                                    value="<" reRender="AccntTable"  
                                    status="ajaxStatus" />

                <apex:commandButton action="{!next}" value=">"
                                    reRender="AccntTable" 
                                    status="ajaxStatus" />

                <apex:commandButton action="{!lastPage}" 
                                    value=">>|"
                                    reRender="AccntTable"  
                                    status="ajaxStatus" />

            </apex:pageBlockButtons>

    <apex:pageBlockTable value="{!Accounts}" 
                         var="item" id="AccntTable">

                        <apex:column value="{!item.ID}"/>
                        <apex:column value="{!item.Name}"/>
                        <apex:column value="{!item.Phone}"/>

            </apex:pageBlockTable>

   
   </apex:pageBlock>
  
  </apex:form>
  
</apex:page>

 
Best Answer chosen by divya manohar
@Karanraj@Karanraj
You have to assign the Standardsetcontroller value to the AccSetController but you have re-initialized the  variable again in the getter property. Try with the below updated controller code below
public class ListViewDemo
{

   string basequery='Select ID, Name,Phone FROM Account ORDER BY NAME ASC';
  
   private Integer pageSize = 10;
   
   public String AccFilterId {get; set;}

   public ListViewDemo(){}


   public ApexPages.StandardSetController AccSetController  //this is a property
   {
     get
        {
          if (AccSetController == null)
          {
            AccSetController=new ApexPages.StandardSetController(Database.getQueryLocator(basequery));
     
            AccSetController.setPageSize(pageSize);

            if (AccFilterId != null)
            {
              AccSetController.setFilterID(AccFilterId);
            }
          }
           
          return AccSetController;
        }
     set;
   }
   
public ListViewDemo(ApexPages.StandardSetController c){
}


   //Navigate to first Page
    public void firstPage()
    {
      AccSetController.first();

    }

    //Navigate to last Page
    public void lastPage()
    {
      AccSetController.last();
    }


    //Navigate to Next page

    public void next()
    {
      if(AccSetController.getHasNext())
      {
        AccSetController.next();
      }
    }


    //Navigate to Prev Page
    public void prev()
    {
      if(AccSetController.getHasPrevious())
      {
        AccSetController.previous();
      }
    }
    
    public SelectOption[] getAccountExistingViews()
    {
     return AccSetController.getListViewOptions();
    }
    
    
    public List<Account> getAccounts()
    {
        return AccSetController.getRecords();
    }

    
    public PageReference resetFilter()
    {
        AccSetController = null;
        
        AccSetController.setPageNumber(1);
        
        return null;
    }
}

 

All Answers

@Karanraj@Karanraj
You have to assign the Standardsetcontroller value to the AccSetController but you have re-initialized the  variable again in the getter property. Try with the below updated controller code below
public class ListViewDemo
{

   string basequery='Select ID, Name,Phone FROM Account ORDER BY NAME ASC';
  
   private Integer pageSize = 10;
   
   public String AccFilterId {get; set;}

   public ListViewDemo(){}


   public ApexPages.StandardSetController AccSetController  //this is a property
   {
     get
        {
          if (AccSetController == null)
          {
            AccSetController=new ApexPages.StandardSetController(Database.getQueryLocator(basequery));
     
            AccSetController.setPageSize(pageSize);

            if (AccFilterId != null)
            {
              AccSetController.setFilterID(AccFilterId);
            }
          }
           
          return AccSetController;
        }
     set;
   }
   
public ListViewDemo(ApexPages.StandardSetController c){
}


   //Navigate to first Page
    public void firstPage()
    {
      AccSetController.first();

    }

    //Navigate to last Page
    public void lastPage()
    {
      AccSetController.last();
    }


    //Navigate to Next page

    public void next()
    {
      if(AccSetController.getHasNext())
      {
        AccSetController.next();
      }
    }


    //Navigate to Prev Page
    public void prev()
    {
      if(AccSetController.getHasPrevious())
      {
        AccSetController.previous();
      }
    }
    
    public SelectOption[] getAccountExistingViews()
    {
     return AccSetController.getListViewOptions();
    }
    
    
    public List<Account> getAccounts()
    {
        return AccSetController.getRecords();
    }

    
    public PageReference resetFilter()
    {
        AccSetController = null;
        
        AccSetController.setPageNumber(1);
        
        return null;
    }
}

 
This was selected as the best answer
divya manohardivya manohar
Hi it works.
But I want to call this page in Account, List view i.e I want to add a custom button, content source= name of visual force page.
But I am not able to see the vf page in content.
 
divya manohardivya manohar
Hi
I got the answer. In VF page we need to have a standard controller with extension class.
Also we need to use recordSetVar attribute.