Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF converter for selectOneMenu
    primarykey
    data
    text
    <p>I want to set a many-to-one relationship in a hibernate entity using <code>selectOneMenu</code>.</p> <pre><code>&lt;h:selectOneMenu value="#{taskBean.instance.currency}" class="form-control" id="currencyList" converter="#{currencyConverterBean}"&gt; &lt;f:selectItems value="#{currencyBean.currencyList}" var="currency" itemLabel="#{currency.name}"/&gt; &lt;/h:selectOneMenu&gt; </code></pre> <p>where <code>#{taskBean.instance}</code> returns a hibernate entity Task:</p> <pre><code>@Entity @Table(name="task") public class Task { //... public Currency currency; @ManyToOne @JoinColumn(name = "currencyID") public Currency getCurrency() { return currency; } //... } </code></pre> <p>If one sets value <code>#{taskBean.instance.currency.id}</code> for selectOneMenu and <code>#{currency.id}"</code> for itemValue it doesn't work. Therefore, I tryed to use a converter:</p> <pre><code>@ManagedBean(name="currencyConverterBean") @FacesConverter(value = "currencyConverter") @RequestScoped public class CurrencyConverter implements Converter { CurrencyBO currencyBO; @Override public Currency getAsObject(FacesContext ctx, UIComponent component, String value) { return getCurrencyBO().getCurrencyById(new Integer(value)); } @Override public String getAsString(FacesContext fc, UIComponent uic, Object o) { return String.valueOf(((Currency) o).getId()); } public CurrencyBO getCurrencyBO() { return currencyBO; } public void setCurrencyBO(CurrencyBO currencyBO) { this.currencyBO = currencyBO; } } </code></pre> <p>It doesn't work. It returns the following error:</p> <pre><code>PWC1406: Servlet.service() for servlet Faces Servlet threw exception java.lang.NullPointerException at com.otv.converter.CurrencyConverter.getAsObject(CurrencyConverter.java:33) </code></pre> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload