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
balu palavalasabalu palavalasa 

error in adding select options

hi All,

I've witten a code which has to take input from an input box and add the input to select options until submit button is pressed.
that is as long as i give input and click on add button it should keep on adding the given input texts to select option list. and if i click on submit button it should display the selectoptions list.

vf code :

<apex:page Controller="selectlistoption">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
         
            <apex:outputlabel value="Enter Country"/>
            <apex:inputText value="{!str}"/>
            <apex:commandButton value="Add" action="{!setCountry}"/> 
            <apex:commandButton value="submit" rerender="showdetails"/>
        </apex:pageBlockSection>    
        
    <apex:pageBlockSection id="showdetails">
      
            <apex:selectlist size="1" value="{!str}">
           
                <apex:selectoptions value="{!country}"/>
                 </apex:selectlist>
        
 
        
        </apex:pageBlockSection>
                    </apex:pageBlock>
    </apex:form>
 
</apex:page>

Controller :

public class selectlistoption {
    
  
      public   string str{get;set;}
    
       public  list<selectoption> country{get;set;}
    
    public pagereference setCountry()
    {
         
        country.add(new selectoption(str,str));
        return null;
       
    }
    
   

}

Error :

System.NullPointerException: Argument 1 cannot be null
Error is in expression '{!setCountry}' in component <apex:commandButton> in page selectlist: Class.selectlistoption.setCountry: line 11, column 1
Class.selectlistoption.setCountry: line 11, column 1
Pradeep SinghPradeep Singh
Hi, Please use below code:-
<apex:page Controller="selectlistoption">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >        
            <apex:outputlabel value="Enter Country"/>
            <apex:inputText value="{!str}" label="Country"/>
            <apex:commandButton value="Add" action="{!addCountry}" reRender="None"/> 
            <apex:commandButton value="submit" rerender="showdetails"/>
        </apex:pageBlockSection>    
        
        <apex:pageBlockSection id="showdetails">      
            <apex:selectlist size="1" value="{!selectedstr}">           
                <apex:selectoptions value="{!country}"/>
            </apex:selectlist>       
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
 
</apex:page>

-----------------
public class selectlistoption {
    
  
      public string str{get;set;}  
      public string selectedstr{get;set;}   
      public list<selectoption> country{get;set;}
       
    public selectlistoption (){
        str = '';
        country = new list<selectoption>();
    }
    
    public void addCountry()
    {
        system.debug('-----' + str);
        country.add(new selectoption(str,str));       
    }
    
   

}