我做了一些编码:转换器,下拉列表,列表,... 我得到一个NullPointerException但我确信所有使用的对象都已初始化。
以下是新编辑的wass ans jsf(转换器位于Fil_M_B bean中,M_B代表Managed Bean)
请你帮我弄清楚现在出了什么问题?
Fil_M_B bean
@Named(value = "fil_M_B")
@SessionScoped
public class Fil_M_B implements Serializable {
@EJB
private FiliereFacadeLocal filiereFacade;
public FiliereFacadeLocal getFiliereFacade() {
return filiereFacade;
}
private List<Filiere> list_fil;
public List<Filiere> getList_fil() {
return list_fil;
}
@PostConstruct
public void list(){
list_fil = filiereFacade.findAll();
}
// NOW THE CONVERTER
@FacesConverter(forClass = Filiere.class)
public static class FiliereControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
Fil_M_B controller = (Fil_M_B) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "Fil_M_B");
return controller.filiereFacade.find(getKey(value));
}
java.lang.String getKey(String value) {
java.lang.String key;
key = value;
return key;
}
String getStringKey(java.lang.String value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Filiere) {
Filiere o = (Filiere) object;
return getStringKey(o.getNomFiliere());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Filiere.class.getName());
}
}
}
/**
* Creates a new instance of Fil_M_B
*/
public Fil_M_B() {
}
Spec_M_B
@Named(value = "spec_M_B")
@SessionScoped
public class Spec_M_B implements Serializable{
@EJB
private SpecialiteFacadeLocal specialiteFacade;
Specialite spec = new Specialite();
public Specialite getSpec() {
return spec;
}
public void setSpec(Specialite spec) {
this.spec = spec;
}
public void ADD(){
specialiteFacade.create(spec);
}
/**
* Creates a new instance of Spec_M_B
*/
public Spec_M_B() {
}
}
Jsf页
<f:view>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Specialite" for="nomSpec" />
<h:inputText id="nomSpec" value="#{spec_M_B.spec.nomSpec}" title="Nom Specialite" required="true" requiredMessage="Required!"/>
<h:outputLabel value="Filiere" for="nomFiliere" />
<h:selectOneMenu id="nomFiliere" value="#{spec_M_B.spec.nomFiliere}" title="Nom Filiere" required="true" requiredMessage="Required !">
<f:selectItems value="#{fil_M_B.list_fil}"/>
</h:selectOneMenu>
</h:panelGrid>
<h:commandButton value="ADD" action="#{spec_M_B.ADD()}"/>
</h:form>
</f:view>
请你帮我弄清楚什么是错的?
堆栈跟踪下面是我在浏览器中看到的堆栈跟踪,单击“添加”按钮
java.lang.NullPointerException
at TEST_3.Fil_M_B.access$000(Fil_M_B.java:27)
at TEST_3.Fil_M_B$FiliereControllerConverter.getAsObject(Fil_M_B.java:59)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1045)
at javax.faces.component.UIInput.validate(UIInput.java:975)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1248)
at javax.faces.component.UIInput.processValidators(UIInput.java:712)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
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:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)