I am trying to get Object "Plage" from h:selectOneMenu ,That's why I created a Converter ,it convert String id to Plage object existing in database using dao (hibernate ) Here my xhtml code :
<h:selectOneMenu id="pays" styleClass="form-control selectpicker" value="#{service.plage}"
converter="Convert" >
<f:selectItems value="#{moyen.plages_l}" var="pl"
itemValue="#{pl}" itemLabel="plage entre #{pl.min} et #{pl.max}" />
</h:selectOneMenu>
Here code of Converter :
@FacesConverter(value="Convert")
public class PlageConverter implements Converter {
@Autowired
PlageBo plageBo;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
// TODO Auto-generated method stub
int id = Integer.valueOf(value);
Plage pl = plageBo.getPlagebyid(id);
return pl;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1,
Object service) {
// TODO Auto-generated method stub
Plage plage_conv = (Plage) service;
String idAsString = String.valueOf(plage_conv.getId_plage());
return idAsString;
}
public PlageBo getPlageBo() {
return plageBo;
}
public void setPlagebo(PlageBo plageBo) {
this.plageBo = plageBo;
}}
I overrite equals() and haschode() in Plage class : public class Plage implements Serializable {
private int id_plage;
private Double min;
private Double max;
private MoyenPaiement_pays moyenPaiement_pays;
//Getters & Setters
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
//return super.equals(arg0);
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Plage that = (Plage) o;
if ( id_plage!=that.id_plage) return false;
if (max != null ? !max.equals(that.max) : that.max != null) return false;
if (min != null ? !min.equals(that.min) : that.min != null) return false;
if (!moyenPaiement_pays.equals(that.moyenPaiement_pays)) return false;
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int result = max.hashCode();
result = 31 * result + (min != null ? min.hashCode() : 0);
return result;
}
When I try to get the variable plage in Bean I got this error :
1100: JSF1073 : javax.faces.FacesException intercepté durant le traitement de PROCESS_VALIDATIONS 3 : UIComponent-ClientId=, Message=null
java.util.logging.ErrorManager: 5
java.lang.NullPointerException
at java.util.PropertyResourceBundle.handleGetObject(Unknown Source)
at java.util.ResourceBundle.getObject(Unknown Source)
at java.util.ResourceBundle.getString(Unknown Source)
at java.util.logging.Formatter.formatMessage(Unknown Source)