JSF Converter给出空指针异常

时间:2013-12-11 11:44:10

标签: jsf javabeans converter

我已阅读与我的问题相关的所有帖子,但我无法找到解决方案。我创建了转换器到我的Country实体,但是当我提交表单时,转换器抛出ConverterException:

Cannot convert 105 to Country java.lang.NullPointerException 105

其中105是我在表单中选择的国家/地区实体的ID。

<h:outputText value="Country (*): " />
<h:selectOneMenu id="country" value="#{createUser.user.country}" required="true" requiredMessage="Please select a country." converter="#{countryConverter}" >
   <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
  <f:selectItems value="#{createUser.countries}" var="country" itemValue="#{country}" itemLabel="#{country.country}" />
 </h:selectOneMenu>

这是转换器:

@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);
    }
}
}

CreateDao来源:

@Stateless
@LocalBean
public class CountryDAO {

public CountryDAO() {
}
@PersistenceContext
private EntityManager em;
@Resource
SessionContext context;

public List<Country> getAllCountries() {
    TypedQuery<Country> 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<Country> query = em.createNamedQuery(Country.FIND_BY_ID, Country.class).setParameter("id", id);
    return query.getSingleResult();
}
}

如果我在Bean中使用createDao.find(Long.valueOf(105))它可以正常工作。 哪里错了?


问题似乎与转换器有关,因为如果我评论try / catch子句

@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);
//        }
}

并修改DAO以打印与国家/地区实体相关的内容:

 public Country find(Long id) {
    Country c = em.find(Country.class, id);
    System.out.println(c.getCountry());
    return c;
 }

我在控制台中看到:

FINE: SELECT ID, COUNTRY FROM COUNTRY WHERE (ID = ?)    bind => [1 parameter bound]
FINEST: Connection released to connection pool [read].
INFO: Italy

并在浏览器中:

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)

0 个答案:

没有答案