java - 错误java.lang.NullPointerException

标签 java jsf-2

我正在使用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();


该异常试图告诉您someObjectnull。您需要通过在调用时确保它不是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}

关于java - 错误java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7163603/

相关文章:

java - 在 "this"或 Java 中的私有(private)对象上同步?

java - 在作为提案输入 JSR/JEP 之前,是否有一种有意义的方式来收集反馈?

java - 配置pentaho的hdfs-vfs来获取hdfs-site.xml

JSF2、i18n 和 SEO

JSF 组件绑定(bind) - 一些困惑

jsf - 为什么 getter 被渲染属性调用了这么多次?

java - 产生更多线程

java - 如何从 https 地址的 wsdl 在 Eclipse 中生成 Web 服务客户端?

jsf - Primefaces DataTable 的特定于列的上下文菜单

jsf-2 - FacesConverter forClass 不适用于复合组件