JSF Converter oneSelectMenu空指针异常

时间:2013-08-31 09:27:44

标签: java jsf converter

我有一个选择菜单:

<p:selectOneMenu id="specialization" value="#{visitMB.specialization}" effect="fade" 
    style="width:200px" converter="#{specializationConverter}">
        <f:selectItems value="#{visitMB.allSpecialization}" var="specialization" 
            itemValue="#{specialization}" itemLabel="#{specialization.name}"/>
</p:selectOneMenu>

VisitMB:

@ManagedBean
@ViewScoped
public class VisitMB implements Serializable {

    @EJB
    private VisitDaoLocal visitDao;
    @EJB
    private UserDaoLocal userDao;
    @EJB
    private SpecializationDaoLocal specializationDao;
    private Specialization specialization;

    public VisitMB() {
    }

    public Specialization getSpecialization() {
        if (specialization == null) {
            specialization = new Specialization();
        }
        return specialization;
    }

    public void setSpecialization(Specialization specialization) {
        this.specialization = specialization;
    }

    public List<Specialization> getAllSpecialization() {
        return specializationDao.findAllSpecialization();
    }

转换器:

@ManagedBean
@RequestScoped
public class SpecializationConverter implements Converter {

    @EJB
    private SpecializationDaoLocal specializationDao;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Integer id = Integer.parseInt(value);
        return specializationDao.find(id);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((Specialization) value).getId().toString();
    }
}

我有错误:

java.lang.NullPointerException
    at pl.ePrzychodnia.converter.SpecializationConverter.getAsString(SpecializationConverter.java:46)
    at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:71)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeLabel(SelectOneMenuRenderer.java:137)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:92)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:65)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:92)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeBody(PanelGridRenderer.java:60)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:63)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)
    at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:202)
    at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:119)
    at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:56)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at pl.ePrzychodnia.filter.FilterLogin.doFilter(FilterLogin.java:49)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

但我现在不知道为什么会有错误。转换器运行良好,因为当我使用它时:

改变

<p:selectOneMenu id="specialization" value="#{visitMB.specialization}" 
    effect="fade" style="width:200px" converter="#{specializationConverter}">

<p:selectOneMenu id="specialization" value="#{userMB.user.specialization}" 
    effect="fade" style="width:200px" converter="#{specializationConverter}">
一切顺利。为什么?我必须在visitMB中设置专业。

1 个答案:

答案 0 :(得分:3)

这是因为提供的值#{visitMB.specialization}null,您永远不会在转换器中检查它。您的转换器在提供的值上直接调用getId(),而不事先检查它是否为null

在转换器中添加空检查:

return (value != null) ? ((Specialization) value).getId().toString() : null;

请注意,如果Specialization实例具有null ID(在此特定情况下不会发生),则此方法仍会失败。但是你现在应该能够在发生这种情况时自行解决这个问题。

另请注意,此问题基本上与JSF无关。它只是基本的Java。你有一个java.lang包的例外。