title: h:selectOneMenu with user-defined object
body: <p>I'm trying to implement two <code>&lt;h:selectOneMenu/&gt;</code></p>
  
  <p>The first one contains Type 1 object.
  When the user selects a object from this list,
  the second <code>&lt;h:selectOneMenu/&gt;</code> shall be displayed.</p>
  
  <p>Here is my code</p>
  
  <p>xhtml</p>
  
  <pre><code>&lt;h:form&gt;
  &lt;h:selectOneMenu value="#{handler.selectedType1}" valueChangeListener="#{handler.valueChanged}"&gt;
      &lt;f:selectItems value="#{handler.types1}" var="type1" itemValue="#{type1.name}" itemLabel="#{type1.name}" /&gt;
      &lt;a4j:ajax event="valueChange" render="type2List" execute="@this"/&gt;
  &lt;/h:selectOneMenu&gt;
  &lt;h:selectOneMenu id="type2List" value="#{handler.selectedType2}" rendered="#{not empty handler.selectedType2}"&gt;
      &lt;f:selectItems value="#{handler.types2}" var="type2" itemLabel="#{type2.name}" /&gt;
  &lt;/h:selectOneMenu&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>managed bean</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class Handler {
  
      private Type1 selectedType1;
      private List&lt;Type1&gt; types1;
      private Type2 selectedType2;
      private List&lt;Type2&gt; types2;
  
          //getters, setters
  
          public void valueChanged(ValueChangeEvent event) {
          variants.clear();
          if (event.getNewValue() != null) {
              System.out.println("new value: " + event.getNewValue());
                          // Fill the second list
          }
      }
  }
  </code></pre>
  
  <p>Type1 and Type2 are to custom classes. Their name attribute is a string.</p>
  
  <p>The problem is that when I select an element from the first list, I get</p>
  
  <pre><code>    24 févr. 2013 12:15:26 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer getConvertedValue
  GRAVE: Could not instantiate converter for type class com.flightfaq.beans.business.AircraftType: java.lang.NullPointerException
  24 févr. 2013 12:15:26 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer getConvertedValue
  GRAVE: 
  java.lang.NullPointerException
      at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:129)
      at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
      at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
      at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
      at javax.faces.component.UIInput.validate(UIInput.java:960)
      at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
      at javax.faces.component.UIInput.processValidators(UIInput.java:698)
      at org.richfaces.context.PartialViewExecuteVisitCallback.visit(PartialViewExecuteVisitCallback.java:55)
      at org.richfaces.context.BaseExtendedVisitContext.invokeVisitCallback(BaseExtendedVisitContext.java:321)
      at javax.faces.component.UIComponent.visitTree(UIComponent.java:1612)
      at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
      at javax.faces.component.UIForm.visitTree(UIForm.java:371)
      at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
      at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
      at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
      at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
      at org.richfaces.context.ExtendedPartialViewContextImpl.executeComponents(ExtendedPartialViewContextImpl.java:237)
      at org.richfaces.context.ExtendedPartialViewContextImpl.processPartialExecutePhase(ExtendedPartialViewContextImpl.java:217)
      at org.richfaces.context.ExtendedPartialViewContextImpl.processPartial(ExtendedPartialViewContextImpl.java:196)
      at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1167)
      at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
      at java.lang.Thread.run(Thread.java:680)
  </code></pre>
  
  <p>I guess that the valueChangeListener property on the first select element tries to trigger my managed bean method "valueChanged". But it is stopped before reaching it (I printed something inside, and it is not shown in the java console before the exception is thrown)</p>
  
  <p>Is the problem related with the fact that Type1 and Type2 are not Strings ?
  How can I fix this ?</p>
  
  <hr>
  
  <p>EDIT : after BalusC's answer this is my code :</p>
  
  <p>xhtml</p>
  
  <pre><code>    &lt;h:form&gt;
          &lt;h:selectOneMenu value="#{missionHandler.selectedAircraftType}" converter="#{missionHandler.acTypeConverter}"&gt;
              &lt;f:selectItems value="#{missionHandler.aircraftTypes}" var="type" itemValue="#{type}" itemLabel="#{type.typeOACI}" /&gt;
              &lt;f:ajax listener="#{missionHandler.changeSelectedAircraftType}" render="variantsMenu"/&gt;
          &lt;/h:selectOneMenu&gt;
          &lt;h:selectOneMenu id="variantsMenu" value="#{missionHandler.selectedVariant}" converter="#{missionHandler.variantConverter}"&gt;
              &lt;f:selectItems value="#{missionHandler.variants}" var="variant" itemValue="#{variant}" itemLabel="#{variant.commercialName}" /&gt;
          &lt;/h:selectOneMenu&gt;
      &lt;/h:form&gt;
  </code></pre>
  
  <p>managed bean</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class MissionHandler implements Serializable {
  
      private static final long serialVersionUID = 2462652101518266609L;
  
      private Mission mission;
  
      @EJB
      private AircraftTypeDao aircraftTypeDao;
  
      @EJB
      private VariantDao variantDao;
  
      private AircraftType selectedAircraftType;
  
      private List&lt;AircraftType&gt; aircraftTypes;
  
      private Variant selectedVariant;
  
      private List&lt;Variant&gt; variants;
  
      private AircraftTypeConverter acTypeConverter;
  
      private VariantConverter variantConverter;
  
      public MissionHandler() {
          /** Create an empty mission */
          mission = new Mission();
      }
  
      @PostConstruct
      public void init() {
          aircraftTypes = aircraftTypeDao.findAll();
      }
  
  
      public void changeSelectedAircraftType(AjaxBehaviorEvent event) {
          System.out.println("Listener call");
          variants=variantDao.find(selectedAircraftType);
          System.out.println("Listener out");
      }
  }
  </code></pre>
  
  <p>converters</p>
  
  <pre><code>@FacesConverter(forClass=AircraftType.class)
  public class AircraftTypeConverter implements Converter, Serializable {
  
      private static final long serialVersionUID = 7053414108213420057L;
  
      private final AircraftTypeDao aircraftDao = new AircraftTypeDaoImpl();
  
      @Override
      public Object getAsObject(FacesContext context, UIComponent component,
              String value) {
          System.out.println("convert to Object " + value);
          AircraftType acType = aircraftDao.find(value);
          System.out.println("got ac type " + acType.getTypeOACI());
          return acType;
      }
  
      @Override
      public String getAsString(FacesContext context, UIComponent component,
              Object value) {
          System.out.println("convert to String " + value);
          return ((AircraftType) value).getTypeOACI();
      }
  
  }
  
  @FacesConverter(forClass=Variant.class)
  public class VariantConverter implements Converter, Serializable {
  
      private static final long serialVersionUID = 7053414108213420057L;
  
      private final VariantDao variantDao = new VariantDaoImpl();
  
      @Override
      public Object getAsObject(FacesContext context, UIComponent component,
              String value) {
          System.out.println("convert to Object " + value);
          Variant variant = variantDao.find(value);
          System.out.println("got variant " + variant.getCommercialName());
          return variant;
      }
  
      @Override
      public String getAsString(FacesContext context, UIComponent component,
              Object value) {
          System.out.println("convert to String " + value);
          return ((Variant) value).getCommercialName();
      }
  
  }
  </code></pre>
  
  <p>When I execute this code:
  - the list of aircraft types is correclty loaded from the DB
  - when I select one, it is correclty converted from string to AircraftType (thanks to the converter)
  BUT that's all. No call to the listener method, and no update of the "Variant" selectOneMenu</p>
  
  <p>I still need your help :)</p>