5

So i am using seam to try to make a dropdown menu to add a delivery to a database. I'm using a drop down menu to select which employee from the database is doing the delivery. The menu loads all the employees in the database just fine but when I select one and click add, I get the error "Conversion Error setting value '(hash for employee)' for 'null Converter'.

here's the code for the dropdown menu:

<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}" required="false">
                    <f:selectItem itemValue="#{null}" itemLabel="Selct One"/>
                    <s:selectItems value="#{deliveryPort.empList}" var="emp" label="#   {emp.employeeId} #{ emp.nameFirst}"/> 
               </my:dropdown>

Any help would be greatly appreciated. Thank you

user1423793
  • 279
  • 2
  • 6
  • 16

2 Answers2

7

You should do 2 things to avoid this error:

  1. Make sure your class emp is implementing equals() and hashCode()
  2. Use an converter to convert your selected value before passing it to the backing bean

BalusC wrote a nice tutorial about converters and how to use them.

flash
  • 6,730
  • 7
  • 46
  • 70
0

I implemented the converter and equals() & hashCode() in Employee class (class of emp) according to the answer of @flash.

Part of XHTML:

<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}" required="false">
    <f:selectItem itemValue="#{null}" itemLabel="Selct One"/>
    <s:selectItems value="#{deliveryPort.empList}" var="emp" label="#   {emp.employeeId} #{ emp.nameFirst}"/> 
    <f:converter converterId="empConverter" />
</my:dropdown>

EmployeeConverter Class:

package mypackage.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("empConverter")
public class EmployeeConverter implements Converter
{

    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        return value;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        return value.toString();
    }

}

Part of Employee Class (the class of emp, supposedly employeeId is String):

public boolean equals(Object other)
{
    return other instanceof Employee && (employeeId != null) ? employeeId.equals(((Employee) other).employeeId) : (other == this);
}

public int hashCode()
{
    return employeeId != null ? this.getClass().hashCode() + employeeId.hashCode() : super.hashCode();
}

public String toString()
{
    return "Employee[" + employeeId + "," + nameFirst + "]";
}

I had a similar problem. It worked for me.

Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101