0

我的项目有以下错误

严重:为组件“loginButton”调用动作侦听器“#{tableBean.addNew}”时收到“java.lang.NullPointerException”严重:java.lang.NullPointerException

这是我的 addNew 函数:

public void addNew(ActionEvent actionEvent) throws SQLException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        initiate_ddate=sdf.format(initiateddate);
        teststart_date=sdf.format(teststartdate);
        scheduledend_date=sdf.format(scheduledenddate);
        revised_end=sdf.format(revisedend);


        System.err.println("erorrrrrrrrrr");

        Connection connadd = new Connector().getConn();
        Statement stmt = connadd.createStatement();

        String sql2 = "INSERT INTO status (`name`, `initiateddate`, `agreementstatus`, `testingdoc`, `teststartdate`, `scheduledenddate`, `teststatus`, `revisedend`, `launcstatus`) VALUES (  '" + name + "'  , '" + initiate_ddate + "' , '" + agreement_status + "', '" + testing_doc + "', '" + teststart_date + "', '" + scheduledend_date + "', '" + test_status + "', '" + revised_end + "', '" + launc_status + "' );  ";

        stmt.executeUpdate(sql2);
        FacesMessage msg = new FacesMessage("Project Edited", name + " " + initiate_ddate + " " + agreement_status + " " + testing_doc + " " + teststart_date + " " + scheduledend_date + " " + test_status + " " + revised_end + " " + launc_status);

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

这是什么原因?

这是堆栈跟踪:

SEVERE: Received 'java.lang.NullPointerException' when invoking action listener '#{tableBean.addNew}' for component 'loginButton'
SEVERE: java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1070)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:859)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:852)
    at java.text.DateFormat.format(DateFormat.java:316)
    at table.TableBean.addNew(TableBean.java:192)
    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 com.sun.el.parser.AstValue.invoke(AstValue.java:254)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIData.broadcast(UIData.java:1093)
    at javax.faces.component.UIData.broadcast(UIData.java:1093)
    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:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    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:619)

SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=form_tab:tabView, Message=/index.xhtml @234,223 actionListener="#{tableBean.addNew}": java.lang.NullPointerException
SEVERE: /index.xhtml @234,223 actionListener="#{tableBean.addNew}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /index.xhtml @234,223 actionListener="#{tableBean.addNew}": java.lang.NullPointerException
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIData.broadcast(UIData.java:1093)
    at javax.faces.component.UIData.broadcast(UIData.java:1093)
    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:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    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:619)
Caused by: java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1070)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:859)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:852)
    at java.text.DateFormat.format(DateFormat.java:316)
    at table.TableBean.addNew(TableBean.java:192)
    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 com.sun.el.parser.AstValue.invoke(AstValue.java:254)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
    ... 35 more
4

1 回答 1

1

NullpointerExceptions 通常很容易解决,在这种情况下也是如此:

诀窍是在堆栈跟踪中查找您拥有的一段代码

SEVERE: java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1070)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:859)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:852)
    at java.text.DateFormat.format(DateFormat.java:316)
    at table.TableBean.addNew(TableBean.java:192)

在这种情况下,它是 table.TableBean.addNew 方法在 DateFormat 上执行格式

你正在做其中的 4 个(我不知道第 192 行是什么):

 initiate_ddate=sdf.format(initiateddate);
 teststart_date=sdf.format(teststartdate);
 scheduledend_date=sdf.format(scheduledenddate);
 revised_end=sdf.format(revisedend);

根本原因是以下变量之一为空。它被传递给格式化函数并抛出一个 NPE

  • 发起日期
  • 测试开始日期
  • 预定结束日期
  • 修改结束
于 2013-02-05T06:43:45.810 回答