错误 java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7163603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:54:19  来源:igfitidea点击:

Error java.lang.NullPointerException

javajsf-2

提问by rym

I'am using JSF/primefaces I have an error wich is odd ,beacause I'am calling the same methode from another form and works fine but from this form generate an error!

我正在使用 JSF/primefaces 我有一个奇怪的错误,因为我从另一个表单调用相同的方法并且工作正常但是从这个表单生成一个错误!

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

the error is:

错误是:

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

回答by BalusC

Look at the bottommost root cause of the stacktrace:

查看堆栈跟踪的最底层根本原因:

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

Open DAOKPI.TestAjaxclass and go to createQueryOracle2()method. Line 1201 shall look something like this:

打开DAOKPI.TestAjax类并转到createQueryOracle2()方法。第 1201 行应如下所示:

someObject.doSomething();

This exception is trying to tell you that someObjectis null. You need to fix it by making sure that it's notnullat the point you call it. For example, by just instantiating it.

这个异常试图告诉你someObjectnull. 您需要通过确保它不在null您调用它的位置来修复它。例如,通过实例化它。

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

You could also just skip the call altogether by checking beforehand if it isn't null.

如果不是,您也可以通过事先检查来完全跳过呼叫null

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

Or if it represents a managed bean property or some dependency injection property, then make sure that the scopes are right and that the injector has done its job.

或者,如果它代表托管 bean 属性或某些依赖项注入属性,则确保范围正确并且注入器已完成其工作。



Unrelatedto the concrete problem, a managed bean class with over 1201 lines is really a design smell. Consider refactoring. Form fields for example can go in an entity/javabean class. Business tasks can go in a service class. Database interaction tasks can go in a DAO class. All which can be properties of the managed bean.

具体问题无关,超过 1201 行的托管 bean 类确实是一种设计味道。考虑重构。例如,表单字段可以放在实体/javabean 类中。业务任务可以放在服务类中。数据库交互任务可以放在 DAO 类中。所有这些都可以是托管 bean 的属性。

Uppercased package names is also not really conform the Java Naming Conventions. Also, direct EL method expressions like #{TestAjax.getMyListProject()}and #{TestAjax.fillReport2()}are ugly as long as you don't need to pass arguments or invoke non-getters, better use #{TestAjax.myListProject}and #{TestAjax.fillReport2}. Finally, JSF managed bean names should start with lowercase as they represents just instances of a class: #{testAjax}.

大写的包名也不符合Java 命名约定。此外,只要您不需要传递参数或调用非吸气剂,像#{TestAjax.getMyListProject()}和 之 类的直接 EL 方法表达式#{TestAjax.fillReport2()}就很丑陋,最好使用#{TestAjax.myListProject}#{TestAjax.fillReport2}。最后,JSF 托管 bean 名称应以小写开头,因为它们仅代表类的实例:#{testAjax}.