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
Tamojita GuhasarkarTamojita Guhasarkar 

Getting Null Pointer exception error while clicking on custom next button on Visuslforce Page

Hello Folks,

I'm trying to add next/previous/first/last - the pageinitiations on a Custom Controller page. But while clicing the button, I'm getting below error 

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!last}' in component <apex:commandButton> in page cus_message: Class.cus_message.last: line 31, column 1
Class.cus_message.last: line 31, column 1


Can someone please help to resolve the same.

Controller Code 

public class cus_message 
{
    List<Account> acts =new List<account>();
   
   public ApexPages.StandardSetController con {
        get;
        set;
    }
   
    public List<Account> getHigh_revenue_List()
    {
        acts =[select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue>50000];
        system.debug('inside high revenue');
        return acts;
    }
    
    Public List<Account> getLow_revenue_List()
    {
       
        acts =[select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL];
        system.debug('inside low revenue');
        return acts;
    }
    
    public void first() {
        this.con.first();
    }

    // returns the last page of records
    public void last() {
        this.con.last();        
    }

    // returns the previous page of records
    public void previous() { 
        this.con.previous();
    }

    // returns the next page of records
    public void next() {
        this.con.next();
    }
   
}

Visualforce Page 

<apex:page controller="cus_message">
<apex:form >
    <apex:pageBlock id="block_ID">
    <apex:pageblockButtons >
        <apex:commandButton value="Next Page" action="{!next}"/>
        <apex:commandButton value="Last Page" action="{!last}"/>
    </apex:pageblockButtons>
    
        <apex:tabPanel >
            <apex:tab label="High Revenue">
                <apex:pageblockTable value="{!High_revenue_List}" var="var1">
                    <apex:column value="{!var1.name}"/>
                    <apex:column value="{!var1.industry}"/>
                    <apex:column value="{!var1.type}"/>
                     <apex:column value="{!var1.AnnualRevenue}"/>
                </apex:pageblockTable>
            </apex:tab>
            <apex:tab label="Zero Revenue">
                <apex:pageblockTable value="{!Low_revenue_List}" var="var2">
                     <apex:column value="{!var2.name}"/>
                    <apex:column value="{!var2.industry}"/>
                    <apex:column value="{!var2.type}"/>
                     <apex:column value="{!var2.AnnualRevenue}"/>
                </apex:pageblockTable>         
                
         
            </apex:tab>
        </apex:tabPanel>
    </apex:pageBlock>
 </apex:form>
</apex:page>
Best Answer chosen by Tamojita Guhasarkar
Ashish KeshariAshish Keshari
Hi Tamojit,
I was working on it - here is the below code which should solve your purpose.
public class cus_message 
{
   private List<Account> acts = new List<account>();   
   private String Query;
   
  	public ApexPages.StandardSetController conLower {
        get {
            if(conLower == null) {
                conLower = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100]));
                // sets the number of records in each page set
                conLower.setPageSize(10);
            }
            return conLower;
        }
        set;
    }
    
  	public ApexPages.StandardSetController conHigher {
        get {
            if(conHigher == null) {
                conHigher = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue > 50000 limit 100]));
                // sets the number of records in each page set
                conHigher.setPageSize(10);
            }
            return conHigher;
        }
        set;
    }
   
    public List<Account> getHigh_revenue_List()
    {
        return (List<Account>)conHigher.getRecords();
    }
    
    Public List<Account> getLow_revenue_List()
    {
        return (List<Account>)conLower.getRecords();
    }
    
    // returns the first page of records    
    public void firstLower() {
        conLower.first();
    }

    // returns the last page of records
    public void lastLower() {
        conLower.last();        
    }

    // returns the previous page of records
    public void previousLower() { 
        conLower.previous();
    }

    // returns the next page of records
    public void nextLower() {
        conLower.next();
    }

    // returns the first page of records    
    public void firstHigher() {
        conHigher.first();
    }

    // returns the last page of records
    public void lastHigher() {
        conHigher.last();        
    }

    // returns the previous page of records
    public void previousHigher() { 
        conHigher.previous();
    }

    // returns the next page of records
    public void nextHigher() {
        conHigher.next();
    }


    
}
 
<apex:page controller="cus_message" sidebar="false">
<apex:form >
    <apex:pageblock >
    	<apex:tabPanel>
        	<apex:tab label="Zero Revenue">
                <apex:pageblockTable value="{!Low_revenue_List}" var="lv">
                    <apex:column value="{!lv.name}"/>
                    <apex:column value="{!lv.industry}"/>
                    <apex:column value="{!lv.type}"/>
                    <apex:column value="{!lv.annualrevenue}"/>                    
                </apex:pageblockTable>
                <apex:panelGrid columns="8">
                    <apex:commandButton value="First" action="{!firstLower}"/>
                    <apex:commandButton value="Next" action="{!nextLower}"/>
                    <apex:commandButton value="Previous" action="{!previousLower}"/>
                    <apex:commandButton value="Last" action="{!lastLower}"/>    
                </apex:panelGrid> 
			</apex:tab>
        	<apex:tab label="High Revenue">
                <apex:pageblockTable value="{!High_revenue_List}" var="hv">
                    <apex:column value="{!hv.name}"/>
                    <apex:column value="{!hv.industry}"/>
                    <apex:column value="{!hv.type}"/>
                    <apex:column value="{!hv.annualrevenue}"/>                    
                </apex:pageblockTable>
                <apex:panelGrid columns="8">
                    <apex:commandButton value="First" action="{!firstHigher}"/>
                    <apex:commandButton value="Next" action="{!nextHigher}"/>
                    <apex:commandButton value="Previous" action="{!previousHigher}"/>
                    <apex:commandButton value="Last" action="{!lastHigher}"/>    
                </apex:panelGrid> 
			</apex:tab>
        </apex:tabPanel>	        
 	</apex:pageblock>
</apex:form>
</apex:page>

Please mark resolved if this solves your purpose. thanks.

All Answers

Ashish KeshariAshish Keshari
Hi Tamojita,
I see the way you have created it - you are not initiating the variable 'con' as when the controller loads - it is still pointing to null.
I think you can see this similar tutorial where it has been implemented - look at the Example.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardsetcontroller.htm
public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}
<apex:page controller="opportunityList2Con">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Amit Singh 1Amit Singh 1
Error is due to that you are calling this.con.next(), this.con.first() like methods and you are not iniliating the memory to contact object. You need to iniliate the Object like con = new Contact();

Refer below links for pagination.

http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/
https://developer.salesforce.com/forums/?id=906F0000000DDAEIA4
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_sosc_pagination.htm
http://www.redpointcrm.com/add-pagination-to-your-visualforce-pages-using-the-soql-offset-clause

Hope this helps :)
Thanks!
Amit Singh
Tamojita GuhasarkarTamojita Guhasarkar
Hello Guys,

Thanks for the response. Ok so now I'm able to creack the Null Pointer erorr , but once I'm running my code, the next button is not working. Basically the query I used, is resturing 100 records, so I'm getting this 100 records on a page hence Next is not in use. I want to display not more than 10 records in a single page, so in my case "Next" should go to atleast 10 times, right ? Can u guys plz check and let me know what I'm missing ?

Controller :
public class cus_message 
{
    List<Account> acts =new List<account>();   
   String Query;
   
  public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator(Query));
                system.debug('the query is '+Query);
                // sets the number of records in each page set
                con.setPageSize(10);
            }
            return con;
        }
        set;
    }
   
    public List<Account> getHigh_revenue_List()
    {
        acts =[select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue>50000 limit 100];
        Query ='select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue>50000 limit 100';
        system.debug('inside high revenue');
        return acts;
    }
    
    Public List<Account> getLow_revenue_List()
    {
       
        acts =[select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100];
        Query ='select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100';
        system.debug('inside low revenue');
        return acts;
    }
    
    public void first() {
        con.first();
        
    }

    // returns the last page of records
    public void last() {
        con.last();        
    }

    // returns the previous page of records
    public void previous() { 
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
        
    }
   
    
}

Visualforce Page:
<apex:page controller="cus_message" sidebar="false">
<Apex:form >
    <apex:commandButton value="Next" action="{!next}"/>
    <apex:pageblock >
           <apex:pageblockTable value="{!Low_revenue_List}" var="lv">
             <apex:column value="{!lv.name}"/>
         </apex:pageblockTable>
 </apex:pageblock>

</Apex:form>
</apex:page>

Note:
1> I want to achieve it by Standaradsetcontrollers only.

Thanks,

 
Ashish KeshariAshish Keshari
Hi Tamojita,
I made quick modification to your apex code and next button is now working and only showing the 10 records at a time.
Compare with your code and you will understand. I can develop the entire code but I want you to learn as well.
Hope this helps. 
public class cus_message 
{
   List<Account> acts =new List<account>();   
   String Query;
   
  public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100]));
                //system.debug('the query is '+Query);
                // sets the number of records in each page set
                con.setPageSize(10);
            }
            return con;
        }
        set;
    }
   
    public List<Account> getHigh_revenue_List()
    {
        acts =[select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue>50000 limit 100];
        Query ='select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue>50000 limit 100';
        system.debug('inside high revenue');
        return acts;
    }
    
    Public List<Account> getLow_revenue_List()
    {
       
        //acts =[select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100];
        //Query ='select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100';
        //system.debug('inside low revenue');
        return (List<Account>)con.getRecords();
    }
    
    public void first() {
        con.first();
        
    }

    // returns the last page of records
    public void last() {
        con.last();        
    }

    // returns the previous page of records
    public void previous() { 
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
        
    }
   
    
}

 
Tamojita GuhasarkarTamojita Guhasarkar
Hi Ashish,

Thanks for pointing to the right direction . Yesm return (List<Account>)con.getRecords() does the magic tric. Ok now I'm stuck on another thing. I'm actually displying multiple list of records on different tabs. So I need to use the next/first/previos/last buttons for each tabs, i.e, each query. 
I'm able to pass the query dinamically , but facing some issue. Hear my undersatnding about the Con variable .
1> initally con is set to null. when I click a tab for the first time , the query will fire and set con with the result set. Now, the result set will be displayed by the getRecords method and next method will dipaly the records in segments.

Now, the issue what I'm facing is, as con is set with one set of records, if I click another tab, the next query is not getting loded. I tried a lot of way to set con to null , but its resulting the next/previous/last/first methods to not functioning and diplaying only the first 10 records. So any way I could set dynamic value in con ?

Controller Code:
public class cus_message 
{

String Query;
String build_query;
   
  public ApexPages.StandardSetController con {
        get {
                       
            if(con == null ) {
      
                con = new ApexPages.StandardSetController(Database.getQueryLocator(build_query));
                system.debug('the query is '+build_query);
                // sets the number of records in each page set
                con.setPageSize(5);
            }
            
            return con;
        }
        set;
    }
   
      
    public List<Account> getHigh_revenue_List()
    {        
       
        
        Query ='select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue>50000';
        build_query =Query;
        system.debug('inside high revenue');       
        return (List<Account>)con.getRecords();
    }
    
    Public List<Account> getLow_revenue_List()
    {       
      
        Query ='select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL';
        build_query =Query;
        system.debug('inside low revenue');        
        return (List<Account>)con.getRecords();

    }
    
    public void first() {
       
        con.first();
         system.debug('the query is '+build_query);
        
    }

    // returns the last page of records
    public void last() {
        
        con.last();  
         system.debug('the query is '+build_query);      
    }

    // returns the previous page of records
    public void previous() { 
        
        con.previous();
    }

    // returns the next page of records
    public void next() {
       
        con.next();
        
    }
    
    
    
}
Tamojita GuhasarkarTamojita Guhasarkar
Hello Guys,

Can anyone help me out on this please..
Ashish KeshariAshish Keshari
Hi Tamojit,
I was working on it - here is the below code which should solve your purpose.
public class cus_message 
{
   private List<Account> acts = new List<account>();   
   private String Query;
   
  	public ApexPages.StandardSetController conLower {
        get {
            if(conLower == null) {
                conLower = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue = NULL limit 100]));
                // sets the number of records in each page set
                conLower.setPageSize(10);
            }
            return conLower;
        }
        set;
    }
    
  	public ApexPages.StandardSetController conHigher {
        get {
            if(conHigher == null) {
                conHigher = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [select name,phone,industry,type,AnnualRevenue from Account where AnnualRevenue > 50000 limit 100]));
                // sets the number of records in each page set
                conHigher.setPageSize(10);
            }
            return conHigher;
        }
        set;
    }
   
    public List<Account> getHigh_revenue_List()
    {
        return (List<Account>)conHigher.getRecords();
    }
    
    Public List<Account> getLow_revenue_List()
    {
        return (List<Account>)conLower.getRecords();
    }
    
    // returns the first page of records    
    public void firstLower() {
        conLower.first();
    }

    // returns the last page of records
    public void lastLower() {
        conLower.last();        
    }

    // returns the previous page of records
    public void previousLower() { 
        conLower.previous();
    }

    // returns the next page of records
    public void nextLower() {
        conLower.next();
    }

    // returns the first page of records    
    public void firstHigher() {
        conHigher.first();
    }

    // returns the last page of records
    public void lastHigher() {
        conHigher.last();        
    }

    // returns the previous page of records
    public void previousHigher() { 
        conHigher.previous();
    }

    // returns the next page of records
    public void nextHigher() {
        conHigher.next();
    }


    
}
 
<apex:page controller="cus_message" sidebar="false">
<apex:form >
    <apex:pageblock >
    	<apex:tabPanel>
        	<apex:tab label="Zero Revenue">
                <apex:pageblockTable value="{!Low_revenue_List}" var="lv">
                    <apex:column value="{!lv.name}"/>
                    <apex:column value="{!lv.industry}"/>
                    <apex:column value="{!lv.type}"/>
                    <apex:column value="{!lv.annualrevenue}"/>                    
                </apex:pageblockTable>
                <apex:panelGrid columns="8">
                    <apex:commandButton value="First" action="{!firstLower}"/>
                    <apex:commandButton value="Next" action="{!nextLower}"/>
                    <apex:commandButton value="Previous" action="{!previousLower}"/>
                    <apex:commandButton value="Last" action="{!lastLower}"/>    
                </apex:panelGrid> 
			</apex:tab>
        	<apex:tab label="High Revenue">
                <apex:pageblockTable value="{!High_revenue_List}" var="hv">
                    <apex:column value="{!hv.name}"/>
                    <apex:column value="{!hv.industry}"/>
                    <apex:column value="{!hv.type}"/>
                    <apex:column value="{!hv.annualrevenue}"/>                    
                </apex:pageblockTable>
                <apex:panelGrid columns="8">
                    <apex:commandButton value="First" action="{!firstHigher}"/>
                    <apex:commandButton value="Next" action="{!nextHigher}"/>
                    <apex:commandButton value="Previous" action="{!previousHigher}"/>
                    <apex:commandButton value="Last" action="{!lastHigher}"/>    
                </apex:panelGrid> 
			</apex:tab>
        </apex:tabPanel>	        
 	</apex:pageblock>
</apex:form>
</apex:page>

Please mark resolved if this solves your purpose. thanks.
This was selected as the best answer