Reputation: 87
I am new to JSP and I need help for calculating difference between 2 dates in seconds, which gets the date input from a form in the "yyyy-MM-dd" format. But while I compile it I get the error. Please help me in correcting it.
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>
Upvotes: 1
Views: 2599
Reputation: 51711
Since, this is a self-submitting form, the first time the page loads T1
and T2
would not be set. So, use an if
block to check for null
s and skip the date arithmetic if parameters are not set. JSP would load fine then.
<%
String dStart = request.getParameter("T1");
String dStop = request.getParameter("T2");
if (dStart != null && dStop != null) {
// rest of the code
}
%>
Once you submit the dates, your date arithmetic code would get executed as well.
Upvotes: 1