You need to sign in to do that
Don't have an account?
Tamojita 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>
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>
I was working on it - here is the below code which should solve your purpose.
Please mark resolved if this solves your purpose. thanks.
All Answers
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
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
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,
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.
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();
}
}
Can anyone help me out on this please..
I was working on it - here is the below code which should solve your purpose.
Please mark resolved if this solves your purpose. thanks.