title: JSF Converter give null pointer exception
body: <p>I had read all post related to my problem but I'm unable to find a solution. I created the converter to my Country entity, but when I submit the form the converter throw the ConverterException:</p>
  
  <pre><code>Cannot convert 105 to Country java.lang.NullPointerException 105
  </code></pre>
  
  <p>where 105 is the ID of the Country entity I selected in the form.</p>
  
  <pre><code>&lt;h:outputText value="Country (*): " /&gt;
  &lt;h:selectOneMenu id="country" value="#{createUser.user.country}" required="true" requiredMessage="Please select a country." converter="#{countryConverter}" &gt;
     &lt;f:selectItem itemValue="#{null}" itemLabel="-- select one --" /&gt;
    &lt;f:selectItems value="#{createUser.countries}" var="country" itemValue="#{country}" itemLabel="#{country.country}" /&gt;
   &lt;/h:selectOneMenu&gt;
  </code></pre>
  
  <p>This is the converter:</p>
  
  <pre><code>@ManagedBean
  @RequestScoped
  @FacesConverter(forClass = Country.class)
  public class CountryConverter implements Converter {
  
  @EJB
  private CountryDAO countryDAO;
  
  @Override
  public String getAsString(FacesContext context, UIComponent component, Object value) {
      if (!(value instanceof Country)) {
          return null;
      }
  
      return String.valueOf(((Country) value).getId());
  }
  
  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
      if (value == null || value.isEmpty()) {
          return null;
      }
      try {
          return countryDAO.find(Long.valueOf(value));
      } catch (Exception e) {
          throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Country %s %d", value, e.toString(), Long.valueOf((value)))), e);
      }
  }
  }
  </code></pre>
  
  <p>The CreateDao source:</p>
  
  <pre><code>@Stateless
  @LocalBean
  public class CountryDAO {
  
  public CountryDAO() {
  }
  @PersistenceContext
  private EntityManager em;
  @Resource
  SessionContext context;
  
  public List&lt;Country&gt; getAllCountries() {
      TypedQuery&lt;Country&gt; query = em.createNamedQuery(Country.FIND_ALL, Country.class);
      return query.getResultList();
  }
  
  public Country find(Long id) {
      return em.find(Country.class, id);
  }
  
  public Country getById(long id) {
      TypedQuery&lt;Country&gt; query = em.createNamedQuery(Country.FIND_BY_ID, Country.class).setParameter("id", id);
      return query.getSingleResult();
  }
  }
  </code></pre>
  
  <p>If I use createDao.find(Long.valueOf(105)) within a Bean it works fine.
  Where I wrong?</p>
  
  <hr>
  
  <p>The problem seems to be related to the converter, because if I comment the try/catch clause </p>
  
  <pre><code>@Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
      if (value == null || value.isEmpty()) {
          return null;
      }
      //try {
          return countryDAO.find(Long.valueOf(value));
  //        } catch (Exception e) {
  //            throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Country %s %d", value, e.toString(), Long.valueOf((value)))), e);
  //        }
  }
  </code></pre>
  
  <p>and modify the DAO to print something related to the country entity:</p>
  
  <pre><code> public Country find(Long id) {
      Country c = em.find(Country.class, id);
      System.out.println(c.getCountry());
      return c;
   }
  </code></pre>
  
  <p>I see in the console:</p>
  
  <pre><code>FINE: SELECT ID, COUNTRY FROM COUNTRY WHERE (ID = ?)    bind =&gt; [1 parameter bound]
  FINEST: Connection released to connection pool [read].
  INFO: Italy
  </code></pre>
  
  <p>and in the browser:</p>
  
  <pre><code>java.lang.NullPointerException
  at Converters.CountryConverter.getAsObject(CountryConverter.java:46)
  at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
  at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
  at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
  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 javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
  at javax.faces.component.UIForm.processValidators(UIForm.java:253)
  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
  at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
  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)
  </code></pre>