我正在尝试转换String to a
Modelo entity using the JSF
Converter`。
我有这样的看法:
<h:outputText value="Modelo*: "/>
<p:selectOneMenu style="width: 300px" value="#{modeloBean.modelo.nome}" id="modelo" converter="modeloConverter">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{modeloBean.listaModelo()}" var="modelo" itemLabel="#{modelo.nome}" itemValue="#{modelo}" />
</p:selectOneMenu>
<p:message for="modelo"/>
而这个Modelo
实体:
@Entity
@Table(name = "MODELO")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Modelo.findAll", query = "SELECT m FROM Modelo m"),
@NamedQuery(name = "Modelo.findById", query = "SELECT m FROM Modelo m WHERE m.id = :id"),
@NamedQuery(name = "Modelo.findByNome", query = "SELECT m FROM Modelo m WHERE m.nome = :nome")})
public class Modelo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@Size(max = 255)
@Column(name = "NOME")
private String nome;
@OneToMany(mappedBy = "modeloId")
private List<Dipositivo> dipositivoList;
// with respectives getters and setters
我试图创建Converter
如下:
@FacesConverter(value="modeloConverter", forClass=Modelo.class)
@SessionScoped
public class ModeloConverter implements Converter{
@EJB
private ModeloFacade modeloFacade = new ModeloFacade();
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value == null || value.isEmpty()) {
return null;
}
try {
Object o = modeloFacade.consultarPorId(Long.valueOf(value));
return o;
} catch (Exception e) {
throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Modelo", value)), e);
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Modelo)) {
return null;
}
return String.valueOf(((Modelo) value).getId());
}
}
但是,下面的行会抛出一个NullPointerException
,表示它modeloFacade
是null
。
Object o = modeloFacade.consultarPorId(Long.valueOf(value));
这是如何引起的,我该如何解决?
它抛出这个:
SEVERE: java.lang.NullPointerException
at bean.AbstractFacade.consultarPorId(AbstractFacade.java:70)
at converter.ModeloConverter.getAsObject(ModeloConverter.java:35)
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 org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:55)
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 org.primefaces.component.panel.Panel.processValidators(Panel.java:284)
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.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)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
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)
SEVERE: at bean.AbstractFacade.consultarPorId(AbstractFacade.java:70)
SEVERE: at converter.ModeloConverter.getAsObject(ModeloConverter.java:35)
SEVERE: at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
SEVERE: at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
SEVERE: at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
SEVERE: at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:55)
SEVERE: at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
SEVERE: at javax.faces.component.UIInput.validate(UIInput.java:960)
SEVERE: at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
SEVERE: at javax.faces.component.UIInput.processValidators(UIInput.java:698)
SEVERE: at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE: at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE: at org.primefaces.component.panel.Panel.processValidators(Panel.java:284)
SEVERE: at javax.faces.component.UIForm.processValidators(UIForm.java:253)
SEVERE: at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE: at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
SEVERE: at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
SEVERE: at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
SEVERE: at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
SEVERE: at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
SEVERE: at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
SEVERE: at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
SEVERE: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE: at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE: at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE: at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
SEVERE: at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
SEVERE: at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
SEVERE: at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE: at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE: at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE: at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE: at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE: at java.lang.Thread.run(Thread.java:722)