+ Start a Discussion
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan 

Binding problem

Apex code:
Public class drop
{
public List<SelectOption> addopt{get;set;}
Public string name{get;set;}
Public void drop()
{
addopt= new List<SelectOption>();
}
Public void addlist()
{
addopt.add(new SelectOption(name,name));
}
}
 Vf page:
<apex:page controller="drop">
<apex:form >
<apex:inputField value="{!name}"/>
<apex:selectList id="lis">
<apex:selectOptions value="{!addOpt}">
</apex:selectOptions>
</apex:selectList><p/>
<apex:commandButton action="{!addlist}" value="Save" reRender="lis"/>
</apex:form>
</apex:page>

Error:Error: Could not resolve the entity from <apex:inputField> value binding '{!name}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable

Guys help me out
Best Answer chosen by Vidhyasagaran Muralidharan
Virendra ChouhanVirendra Chouhan
Hi Vidhyasagaran Muralidharan,

     Anoop, is right use <apex:inputText> insted of <apex:inputField>.
    And please remove ' Void ' return from constractor . It will work fine.
 
Apex Controller-
Public class drop
{
public List<SelectOption> addopt{get;set;}
Public string name{get;set;}
Public drop()
{
addopt= new List<SelectOption>();
}
Public void addlist()
{
addopt.add(new SelectOption(name,name));
}
}
Vf Page-
<apex:page controller="drop">
  <apex:form >
       <apex:inputText value="{!name}"/>
            <apex:selectList id="lis">
                <apex:selectOptions value="{!addOpt}">
                </apex:selectOptions>
            </apex:selectList><p/>
         <apex:commandButton action="{!addlist}" value="Save" reRender="lis"/>
    </apex:form>
</apex:page>


Viru


All Answers

Anoop yadavAnoop yadav
Hi,

Use <apex:inputText value="{!name}"/> as it is a variable.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
now its fine.
but if i enter a text in visualforce text box it is not adding in the picklist
Error isSystem.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!addlist}' in component <apex:commandButton> in page dropdown
Virendra ChouhanVirendra Chouhan
Hi Vidhyasagaran Muralidharan,

     Anoop, is right use <apex:inputText> insted of <apex:inputField>.
    And please remove ' Void ' return from constractor . It will work fine.
 
Apex Controller-
Public class drop
{
public List<SelectOption> addopt{get;set;}
Public string name{get;set;}
Public drop()
{
addopt= new List<SelectOption>();
}
Public void addlist()
{
addopt.add(new SelectOption(name,name));
}
}
Vf Page-
<apex:page controller="drop">
  <apex:form >
       <apex:inputText value="{!name}"/>
            <apex:selectList id="lis">
                <apex:selectOptions value="{!addOpt}">
                </apex:selectOptions>
            </apex:selectList><p/>
         <apex:commandButton action="{!addlist}" value="Save" reRender="lis"/>
    </apex:form>
</apex:page>


Viru


This was selected as the best answer
Anoop yadavAnoop yadav
Try the below code. it will add the value.

// Controller
Public class drop {

    List<SelectOption> addopt= new List<SelectOption>();
    Public string name{get; set; }
    Public string selectedVal{get; set; }
    Public string pickValue{get; set; }
      
    Public List<SelectOption> getpickValues(){
        return addopt;       
    }

    Public void addlist(){        
        if(name!= null){
            addopt.add(new SelectOption(name,name));
        }        
    }
}

<apex:page controller="drop">
<apex:form>
    <apex:inputText value="{!name}"/> <br/>
    <apex:selectList value="{!pickValue}" id="lis" size="1">
        <apex:selectOptions value="{!pickvalues}">
        </apex:selectOptions>
    </apex:selectList><p/>
    <apex:commandButton action="{!addlist}" value="Save" reRender="lis"/>
</apex:form>
</apex:page>