+ Start a Discussion
Krishnan MishraKrishnan Mishra 

Why i am not able to get value from a picklist on visualforce page to my controller?

Here on this visualforce page i want to select number of records from the picklist given in the left bottom of the page and then display number of records as per it but, I am not able to fetch the value from picklist to my controller.Following is my visualforce and controller code:
VisualForce code:
<apex:page controller="ContactListViewController">
<apex:form >
<apex:PageBlock >
    <apex:PageBlockTable value="{!ContactList}" var="contacts">
    <apex:column headerValue="Name">
        <apex:outputLink value="/{!contacts.id}">
        {!contacts.name}
        </apex:outputLink>
    </apex:column>
    <apex:column headerValue="Account Name">
        <apex:outputLink value="/{!contacts.account.id}">
            {!contacts.account.name}
        </apex:outputLink>
    </apex:column>
    <apex:column value="{!contacts.Title}"/>
    <apex:column value="{!contacts.Phone}"/>
    <apex:column value="{!contacts.email}"/>
    <apex:inlineEditSupport />
    </apex:PageBlockTable>
    <!-- below code for pagination -->
    <div align = "center">
           <!-- To return to first page of records-->
           <apex:commandButton action="{!stdSetController.first}" value="<<" title="First Page" disabled="{!!stdSetController.HasPrevious}"/>
           <!-- To return to Previous page of records-->
           <apex:commandButton action="{!stdSetController.previous}" value="Previous" disabled="{!!stdSetController.HasPrevious}"/>
           <!-- To return to next page of records-->
           <apex:commandButton action="{!stdSetController.next}" value="Next >" disabled = "{!!stdSetController.HasNext}"/>
           <!-- To return to last page of records-->
            <apex:commandButton action="{!stdSetController.last}" value=">>" title="Last Page" disabled="{!!stdSetController.HasNext}"/>
           <!-- InputText to display current page and to navigate to any page number, At righmost side of page-->
           <span style="float:right">
                <apex:outputLabel value="Page ">
                </apex:outputLabel>
                <apex:InputText value="{!PageNumber}" maxLength="4" size="1"/>
                <!-- To navigate to the page-->
                
                <apex:outputLabel value=" of {!stdSetController.PageNumber}">
                </apex:outputLabel>
            </span>
            <!-- To display a list for number of records to be selected per page-->
            <span style = "float:left">
                <apex:SelectList value="{!RecordsPerPageslist}" size="1">
                    <apex:selectOptions value="{!RecordsPerPageOptionList}">
                    </apex:selectOptions>
                </apex:SelectList>
            </span>
            </div>

</apex:PageBlock>
</apex:form>
</apex:page>

Controller code:
public class ContactListViewController {
    list<contact> con = [SELECT Name,Account.name,Title,Phone,Email FROM Contact];
        public ApexPages.StandardSetController stdSetController{
        get{
        stdSetController = new ApexPages.StandardSetController(con);
                return stdSetController;            
        }
        set;//what is use of this set, if we remove this then error is displayed!. 
        //The value is already set in the getter we just need to use that value not setting it
    }
    public Integer PageNumber{
        get{
            this.PageNumber=stdSetController.getPageNumber();
            return this.PageNumber;
        }
        set{
            stdSetController.setPageNumber(value);
        }
    }
    public Integer MaxNumberOfRecords{
        get{
        return stdSetController.getRecords().size();
        }
        set;
    }
    public list<SelectOption> getRecordsPerPageOptionList(){
        list<selectOption> options = new list<SelectOption>();
            options.add(new selectOption('10','10'));
            options.add(new selectOption('25','25'));
            options.add(new selectOption('50','50'));
            options.add(new selectOption('100','100'));
            options.add(new selectOption('200','200'));
            return options;
    }
    public Integer RecordsPerPageslist{
        get;set{
            if(value==null) 
                this.RecordsPerPageslist=10;
             else
                 this.RecordsPerPageslist=value;
        }    
    }
    public list<contact> getcontactList(){ 
       stdSetController.setPageSize(RecordsPerPageslist);
        return (list<contact>)stdSetController.getRecords();
    }
}

The error displayed on my page is:
Visualforce Error
Help for this Page
System.NullPointerException: Argument 1 cannot be null 
Class.ContactListViewController.getcontactList: line 41, column 1
Best Answer chosen by Krishnan Mishra
Amit Singh 1Amit Singh 1
Try below code for the apex. You are getting the Error because of "RecordsPerPageslist​" variable in stdSetController.setPageSize(RecordsPerPageslist);​ RecordsPerPageslist variable is null. 
public class ContactListViewController {
        
        public ApexPages.StandardSetController stdSetController {get; set; }
        
        public ContactListViewController(){
            list<contact> con = [SELECT Name,Account.name,Title,Phone,Email FROM Contact];
            stdSetController = new ApexPages.StandardSetController(con);
            System.debug('#### stdSetController  '+stdSetController );
        }

        
    public Integer PageNumber{
        get{
            this.PageNumber=stdSetController.getPageNumber();
            return this.PageNumber;
        }
        set{
            stdSetController.setPageNumber(value);
        }
    }
    public Integer MaxNumberOfRecords{
        get{
        return stdSetController.getRecords().size();
        }
        set;
    }
    public list<SelectOption> getRecordsPerPageOptionList(){
        list<selectOption> options = new list<SelectOption>();
            options.add(new selectOption('10','10'));
            options.add(new selectOption('25','25'));
            options.add(new selectOption('50','50'));
            options.add(new selectOption('100','100'));
            options.add(new selectOption('200','200'));
            return options;
    }
    public Integer RecordsPerPageslist{
        get;set{
            if(value==null) 
                this.RecordsPerPageslist=10;
             else
                 this.RecordsPerPageslist=value;
        }    
    }
    public list<contact> getcontactList(){ 
        stdSetController.setPageSize(10);
        return (list<contact>)stdSetController.getRecords();
    }
}


 

All Answers

MagulanDuraipandianMagulanDuraipandian
Hi,
Declare the variable for setting values for contactList. Since contactList is null, you are getting this issue.

--
Magulan Duraipandian
www.infallibletechie.com
Krishnan MishraKrishnan Mishra
In the above code as i delete stdSetController.setPageSize(RecordsPerPageslist);
The records are displayed but the functionality of list is not there
Amit Singh 1Amit Singh 1
Try below code for the apex. You are getting the Error because of "RecordsPerPageslist​" variable in stdSetController.setPageSize(RecordsPerPageslist);​ RecordsPerPageslist variable is null. 
public class ContactListViewController {
        
        public ApexPages.StandardSetController stdSetController {get; set; }
        
        public ContactListViewController(){
            list<contact> con = [SELECT Name,Account.name,Title,Phone,Email FROM Contact];
            stdSetController = new ApexPages.StandardSetController(con);
            System.debug('#### stdSetController  '+stdSetController );
        }

        
    public Integer PageNumber{
        get{
            this.PageNumber=stdSetController.getPageNumber();
            return this.PageNumber;
        }
        set{
            stdSetController.setPageNumber(value);
        }
    }
    public Integer MaxNumberOfRecords{
        get{
        return stdSetController.getRecords().size();
        }
        set;
    }
    public list<SelectOption> getRecordsPerPageOptionList(){
        list<selectOption> options = new list<SelectOption>();
            options.add(new selectOption('10','10'));
            options.add(new selectOption('25','25'));
            options.add(new selectOption('50','50'));
            options.add(new selectOption('100','100'));
            options.add(new selectOption('200','200'));
            return options;
    }
    public Integer RecordsPerPageslist{
        get;set{
            if(value==null) 
                this.RecordsPerPageslist=10;
             else
                 this.RecordsPerPageslist=value;
        }    
    }
    public list<contact> getcontactList(){ 
        stdSetController.setPageSize(10);
        return (list<contact>)stdSetController.getRecords();
    }
}


 
This was selected as the best answer