我正在使用JSF / primefaces
我有一个错误,它很奇怪,因为我正在从另一种形式调用相同的methode并可以正常工作,但是从这种形式产生错误!
<h:form id="f7">
<p:panel visible="#{TestAjax.form7Visible}" style="width: 800px;">
<h:panelGrid id="panForm7" columns="1">
<h:selectManyListbox value="#{TestAjax.selectedProjects}" id="project7" size="3">
<f:selectItem itemLabel="-- Select Projects -- " itemValue="0"/>
<f:selectItems value="#{TestAjax.getMyListProject()}" />
</h:selectManyListbox>
<h:selectOneMenu id="chartTypeCh" value="#{TestAjax.chartType}">
<f:selectItem itemValue="PieChart" itemLabel="PieChart"/>
<f:selectItem itemValue="BarChart" itemLabel="BarChart"/>
<f:selectItem itemValue="Table" itemLabel="Table"/>
</h:selectOneMenu>
FROM:<p:calendar value="#{TestAjax.date1}" showOn="button" />
TO :<p:calendar value="#{TestAjax.date2}" showOn="button" />
<p:commandButton ajax="false" action="#{TestAjax.fillReport2()}" value="OK"></p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
错误是:
ATTENTION: #{TestAjax.fillReport2()}: java.lang.NullPointerException
javax.faces.FacesException: #{TestAjax.fillReport2()}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
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:409)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
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 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
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:662)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 32 more
Caused by: java.lang.NullPointerException
at DAOKPI.TestAjax.createQueryOracle2(TestAjax.java:1201)
at DAOKPI.TestAjax.fillReport2(TestAjax.java:925)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:737)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:254)
at com.sun.el.parser.AstValue.invoke(AstValue.java:228)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 33 more
最佳答案
查看堆栈跟踪的最根本原因:
Caused by: java.lang.NullPointerException
at DAOKPI.TestAjax.createQueryOracle2(TestAjax.java:1201)
打开
DAOKPI.TestAjax
类,然后转到createQueryOracle2()
方法。第1201行应如下所示:someObject.doSomething();
该异常试图告诉您
someObject
是null
。您需要通过在调用时确保它不是null
来对其进行修复。例如,通过实例化它。if (someObject == null) {
someObject = new SomeObject();
}
someObject.doSomething();
您也可以通过预先检查是否不是
null
来完全跳过该呼叫。if (someObject != null) {
someObject = new SomeObject();
}
或者,如果它表示托管bean属性或某些依赖项注入属性,则请确保作用域正确,并且注入器已完成其工作。
与具体问题无关,具有超过1201行的托管bean类确实是一种设计气味。考虑重构。例如,表单字段可以放在实体/ javabean类中。业务任务可以进入服务类。数据库交互任务可以放在DAO类中。所有这些都可以是托管bean的属性。
大写的程序包名称也不符合Java Naming Conventions。另外,直接的EL方法表达式如
#{TestAjax.getMyListProject()}
和只要您不需要传递参数或调用非获取器,
#{TestAjax.fillReport2()}
都是丑陋的,最好使用#{TestAjax.myListProject}
和#{TestAjax.fillReport2}
。最后,JSF托管bean名称应以小写字母开头,因为它们仅表示类的实例:#{testAjax}
。