JSF 2.0 "Configuration value of the 'raceID' conversion error for 'null Converter' on the Primefaces selection list

advertisements

I am getting the following error when I want to save the data of my picklist

'Group [id=null, groupName=TEST, distinguishedName="distinguishedNameForGroup"' für 'null Converter'.

This is the converter I use for it @FacesConverter(value="groupConverter", forClass=Group.class) public class GroupConverter implements Converter{

@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {
     if (value.trim().equals("")) {
            return null;
     }

    Object ret = null;
    if (component instanceof PickList) {

        Object dualList = ((PickList) component).getValue();
        DualListModel<?> dl = (DualListModel<?>) dualList;
        for (Object o : dl.getSource()) {
            String id = ((Group) o).getDistinguishedName();

            if (value.equalsIgnoreCase(id)) {
                ret = o;
                break;
            }
        }

        if (ret == null)
            for (Object o : dl.getTarget()) {
                String id = ((Group) o).getDistinguishedName();
                if (value.equals(id)) {
                    ret = o;
                    break;
                }
            }
    }
    return ret;
}

@Override
public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    if (value == null || value.equals("")) {
        return "";
    } else {
        String str = "";
        if (value instanceof Group) {
            str = ""
                    + ((Group) value)
                            .getDistinguishedName();
        }
        return str;
    }

}

}

And the code for the pickList:

<p:pickList
    value="#{bean.groups}" var="group"
    itemLabel="#{group.groupName}" itemValue="#{group}"
    converter="groupConverter">
</p:pickList>

I don't understand the error, because in the web I found that the converter is not called, but definitely is. I am running out of ideas. There is no null value or anything. The Group.class have an equal and hashcode method.

Update: I found out that the selectOneMenu makes the error. This is the code for it. There is no converter, that might be the problem. Testing it.

<p:selectOneMenu id="abteilungsleiterrolle"
    value="#{bean.object.group}">
    <f:selectItems value="#{bean.source}" var="q"
        itemValue="#{q}" itemLabel="#{q.groupName}"/>
</p:selectOneMenu>


Try this

 @ViewScope
@FacesConverter(value="pickListConverter")
public class PickListConverter implements Converter{
    Map<String, Object> tmp;
    @PostConstruct
    public void init() {
        tmp = new HashMap<String, Object>();
    }
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return tmp.get(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            tmp.put(value.toString(), value);
            return value.toString();
        }
    }
}

Whit this converter only make sure that your toString() (NameClass and fields and id) for example "Group[id=" + id +"]" are well format but this converter works for any class

And remember change converter name:

<p:pickList
value="#{bean.groups}" var="group"
itemLabel="#{group.groupName}" itemValue="#{group}"
converter="pickListConverter">