2011-08-23 158 views
0

我使用JSF/primefaces 我有一個奇怪的奇怪,因爲我叫從另一個窗體相同的方法和工作正常,但從這種形式產生一個錯誤!錯誤java.lang.NullPointerException

<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 
+0

請向我們展示TestAjax類。 – user219882

回答

3

看堆棧跟蹤的最底部根源:

Caused by: java.lang.NullPointerException 
at DAOKPI.TestAjax.createQueryOracle2(TestAjax.java:1201) 

打開DAOKPI.TestAjax類和去createQueryOracle2()方法。 1201線應是這個樣子:

someObject.doSomething(); 

這個例外是想告訴你,someObjectnull。您需要通過確保它在您稱之爲而不是null的方式進行修復。例如,通過實例化它。

if (someObject == null) { 
    someObject = new SomeObject(); 
} 
someObject.doSomething(); 

你也可以只跳過通話完全通過預先檢查,如果它不是null

if (someObject != null) { 
    someObject = new SomeObject(); 
} 

或者,如果它代表一個管理bean屬性或者一些依賴注入屬性,那麼請確保該範圍是正確的,該注射器已完成其工作。


無關的具體問題,有管理的bean類有超過1201線確實是一個設計的氣味。考慮重構。例如,表單字段可以放在entity/javabean類中。業務任務可以進入服務類。數據庫交互任務可以進入DAO類。所有這些都可以是託管bean的屬性。

大寫的包名也不符合Java Naming Conventions。此外,直接EL方法表達式如#{TestAjax.getMyListProject()}#{TestAjax.fillReport2()}是醜陋的,只要您不需要傳遞參數或調用非getter,就可以更好地使用#{TestAjax.myListProject}#{TestAjax.fillReport2}。最後,JSF託管bean名稱應該以小寫字母開頭,因爲它們只代表一個類的實例:#{testAjax}

相關問題