-1

Please help me to find the solution.

this is the xhtml code:

<p:selectOneMenu value="#{activteBean.act.activiteFamille}" 
    converter="familleAct"
    var="f" required="Une famille est obligatoire" >
    <f:selectItems value="#{activteBean.actFamList}" var="famille" itemLabel="#        {famille.dsgFam}" itemValue="#{famille}"/>  
    <p:column>#{f.refFam}</p:column>  
    <p:column>#{f.dsgFam}</p:column>  
</p:selectOneMenu>   

here is my converter:

@FacesConverter(forClass=ActiviteFamille.class,value="familleAct" )
public class ActiviteFamilleConverter implements Converter {

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String code) {
    if (code.trim().equals("")) {
        return null;
    } else {
        ActiviteFamilleDao actFamDao = new ActiviteFamilleDao();
        List<ActiviteFamille> actFamList = actFamDao.findAll();

        for (ActiviteFamille af : actFamList) {
            if (af.getRefFam().equals(code)) {
                return af;
            }
        }

    }
    return null;
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
    if (value == null || value.equals("")) {
        return "";
    } else {
        return String.valueOf(((ActiviteFamille) value).getRefFam());
    }
}
}

The managed bean:

@ManagedBean(name = "activteBean")
@ViewScoped
public class ActivteBean implements Serializable {

private Activite act = new Activite();
private ActiviteDao actDao = new ActiviteDao();
private List<Activite> actList;
private boolean init;

private ActiviteFamilleDao actFamDao = new ActiviteFamilleDao();
private List<ActiviteFamille> actFamList;


public boolean isInit() {
    act = new Activite();
    actList = actDao.findAll();
    actFamList=actFamDao.findAll();
    return init;
}
....
}

and thank you for your help.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
Youraf
  • 240
  • 4
  • 15

1 Answers1

6

This can happen if the equals() method for ActiveFamille is not implemented properly.

The error message indicates that the selected (and converted) value does not match any of the elements in your list activteBean.actFamList.

Try to debug and set a break point into the equals() method of ActiveFamille and try to find out why it does not match.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • So helpful! I had the same problem, because I haven't had defined equals() method in my "ActiveFamille" class. There was a corresponding item on the list, but Object's default equals() was matching objects by reference, not object properties. Instance of ActiveFamille that was produced by converter was a completly new instance, of course. – Kangur Sep 13 '12 at 16:23
  • In my case, the breakpoints on equals or hashcode never hit ! – Jugal Thakkar Mar 01 '13 at 09:46