作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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>
访问MB:
@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中设置特化。
最佳答案
这是因为提供的值 #{visitMB.specialization}
为 null
并且您从未在转换器中检查该值。您的转换器直接对提供的值调用 getId()
,而不事先检查它是否不 null
。
在转换器中添加空检查:
return (value != null) ? ((Specialization) value).getId().toString() : null;
请注意,如果存在具有 null
ID 的 Specialization
实例,此方法仍然会失败(在这种特定情况下不会发生这种情况)。但现在您应该能够在发生这种情况时自行解决该问题。
另请注意,这个问题与 JSF 根本无关。这只是基本的Java。您遇到了 java.lang
包的异常。
关于java - JSF Converter oneSelectMenu 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18546603/
我有一个 oneSelectMenu: 访问MB: @ManagedBean @ViewScoped public class VisitMB implements Serial
我是一名优秀的程序员,十分优秀!