1
votes

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>
2

2 Answers

1
votes

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">

0
votes

Part of error message " für 'null Converter'." indicates that there is no valid converter found. You can try folowing:

  • If #{bean.groups} resolves to object List<Group> JSF should find matching converter by class as you have specified forClass=Group.class, but i think you don`t have to give your converter a name
  • Try to remove for class attribute to reference it by name
  • You can annotate it with @ManagedBean and specify el for bean e.g. #{groupConverter}

If converter method is called, but error still persists, then converter not found ("null Converter") error can be produced by other jsf component. Keep in mind that if you specify value="#{bean.group}" where bean.group is Group.class object and omit converter, then JSF will lookup for converter annotated with forClass="Group.class". Here if you made converter as bean you have to specify converter for each component.