[SalesForce] how to get value from drop down to the controller using apex SelectOption

Controller Code –

public class ApplicationController {

    public String selectedApplicationId {get;set;}
    public static List<SelectOption> applicationOptions {
    get {
        if(applicationOptions == null) {
            applicationOptions = new List<SelectOption>(); 
            applicationOptions.add(new SelectOption('', 'None'));
        }
        return applicationOptions;
    } set;
}

    public static List<Application__c> apps{
    get {
        if (apps == null) {
            apps = new List<Application__c>();
        }
        return apps;
    }
    set;
}

public ApplicationController() {
    //basically get the contact ID from parameter
    contactID = parameters.get(CONTACT_ID);
     apps = [Select t.Id, t.Type__c, t.Name From Application__c t where t.contactID =: contactID];

        for(Application__c app : apps) {
            applicationOptions.add(new SelectOption(app.Id, app.Type__c + ' - ' + app.Name));
        }
}

public PageReference goToAnotherPage() {
//do something with the selected application ID
}

}

So on my page I have a button which I hit to get the selected application ID. But every time I hit the button it says

"applicationOptions: Validation Error: Value is not valid"

and the drop down is re-rendered with just None as its only value.

Can someone please guide me where am I going wrong? All the examples that I have seen have options being created as part of the getter/setter but I need to dynamically populate the application options.

Best Answer

It seems like you have not initialized applicationOptions list in your constructor where you are adding values to select options, try initializing as

applicationOptions = new List<SelectOption>();  
Related Topic