2013-08-31 69 views
0

我有一個oneSelectMenu:JSF轉換oneSelectMenu空指針異常

<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中設置專業化。

回答

3

這是因爲提供的值#{visitMB.specialization}null而且您從不檢查轉換器中的數據。如果您的轉換器不是null,那麼您的轉換器將直接在提供的值上調用getId()而不事先檢查。

在轉換器添加一個空檢查:

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

注意,如果有一個Specialization實例與null ID(這不會在這種特殊情況下發生的)這種做法仍然會失敗。但是,只要發生這種情況,您現在應該可以自行解決這個問題。

還要注意,這個問題從根本上與JSF無關。這只是基本的Java。你有一個例外java.lang包。