将日期从jsp格式化为servlet时出现空指针异常

时间:2016-03-31 19:41:16

标签: java jsp servlets

在我的jsp页面中,我将此表单提交给servlet后传递日期:

           <div class="form-group">
              <tr>  
                <td>Date</td>  
                <td><input type=date step=7 name="day" required="required"> </td>  
            </tr> 
            </div>

在我的servlet中我写道:

Date day=new Date();
 String startDateStr = request.getParameter("day");
    System.out.println("stsartdate Str"+startDateStr);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
        date=sdf.parse(request.getParameter("startDateStr"));
        System.out.println("date entered is="+date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

它提示我一个空指针异常。

Mar 31, 2016 3:21:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.controller.TravelServlet] in     context with path [/LetsRide] threw exception
java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(Unknown Source)
at java.text.DateFormat.parse(Unknown Source)
at com.controller.TravelServlet.doPost(TravelServlet.java:76)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

但是,print语句在哪里打印从jsp页面以字符串形式提取的日期,它正在打印:

 stsartdate Str2016-03-10

执行try块时抛出空指针异常。请让我离开这个。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的参数命名为&#34; day&#34;,而不是&#34; startDateStr&#34;。代码应该是

date = sdf.parse(startDateStr);