两个日期之间的差异抛出java.lang.NullPointerException

时间:2013-08-04 19:22:00

标签: jsp date

我是JSP的新手,我需要帮助来计算2个日期之间的差异,以秒为单位,从“Yyyy-MM-dd”格式的表单中获取日期输入。但是当我编译它时,我得到了错误。 请帮我纠正。

错误:

java.lang.NullPointerException
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1380)
    at java.text.DateFormat.parse(DateFormat.java:355)
    at org.apache.jsp.datediff2_jsp._jspService(datediff2_jsp.java:105)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    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.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:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    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:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

我的代码如下:

<%@ page language ="java" import="java.sql.*,java.text.SimpleDateFormat, java.util.Date,java.text.*, java.util.Calendar" %>
<html>
<body bgcolor="pink">
<form method="post">
<input  name="T1" value="2013-07-01"/> 
<input  name="T2" value="2013-07-31"/>
<input type="submit" value="Submit" name="B1">
</form>

<%

String dStart = request.getParameter("T1");
String dStop = request.getParameter("T2");

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date d1 = null;
Date d2 = null;

try {
d1 = format.parse(dStart);
d2 = format.parse(dStop);

out.print(d1);

long difference = d2.getTime() - d1.getTime();

long seconds = difference / 1000;

out.print(seconds);

} catch (Exception e) {
e.printStackTrace();
}
%>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

由于这是一个自提交表单,因此首次加载T1并且T2不会设置。因此,如果未设置参数,请使用if块检查null并跳过日期算术。那么JSP会很好地加载。

<%
  String dStart = request.getParameter("T1");
  String dStop = request.getParameter("T2");

  if (dStart != null && dStop != null) {
    // rest of the code
  }
%>

提交日期后,您的日期算术代码也将被执行。