Servlet method in JSP outputs NullPointerException
I have a JSP that calls a method from a Servlet and it gives me aNullPointerException error in my Tomcat 5.5 Container on Windows.
Here is my Servlet Class:package data;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
publicclass DateServletextends HttpServlet
{
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
}
public String fetchDate(HttpServletRequest request)
{
String jspPath = getServletContext().getRealPath(request.getServletPath());
File jspFile =new File(jspPath);
Date lastModified =new Date(jspFile.lastModified());
SimpleDateFormat fmt =new SimpleDateFormat("EEEE, MMMM dd, yyyy, h:mm a(zz)");
return fmt.format(lastModified);
}
}
The JSP (showDate.jsp):<%@ page import="java.io.*, java.text.*, java.util.*, data.*" %>
Last Modified:
<%
String myDate =new DateServlet().fetchDate(request);
out.println(myDate);
%>
Error messages:org.apache.jasper.JasperException: Exception in JSP: /showDate.jsp:9
6:
7: Last Modified:
8: <%
9: String myDate =new DateServlet().fetchDate(request);
10: out.println(myDate);
11: %>
12:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159)
data.DateServlet.fetchDate(DateServlet.java:16)
org.apache.jsp.showDate_jsp._jspService(org.apache.jsp.showDate_jsp:54)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Please advise how I can get this to work.
[3429 byte] By [
teser2a] at [2007-11-27 11:18:57]

# 1
I ended up putting this in a Bean class and hard coding file name in the Bean class and it works but now I would like to have the file name passed as a condtion in the JSP Java Bean call.
Here is how it works with hardcoding the file name in the bean class and calling it in my JSP where it outputs the last modified date and works great:
Last modified Date:
<jsp:useBean id="datebeann" scope="page" class="data.DateBean" />
<jsp:setProperty name="datebeann" property="*" />
<jsp:getProperty name="datebeann" property="mydate" />
Java Bean class:
package data;
import java.io.*;
import java.text.*;
import java.util.*;
public class DateBean
{
private String mydate = "";
public DateBean()
{
this.mydate = fetchDate();
}
public String fetchDate()
{
String jspPath = "C:\\tomcathome\\mydate2.jsp";
File jspFile = new File(jspPath);
Date lastModified = new Date(jspFile.lastModified());
SimpleDateFormat fmt = new SimpleDateFormat("EEEE, MMMM dd, yyyy, h:mm a(zz)");
return fmt.format(lastModified);
}
public String getMydate()
{
return mydate;
}
public void setMydate(String mydate)
{
this.mydate = mydate;
}
}
The above works but now how can I put the filename in the setProperty so I can have a variable in the JSP. I tried all the below and it just prints out a literal value: C:\\tomcathome\\anotherFile.jsp. Please advise.
Last modified Date:
<jsp:useBean id="datebeann" scope="page" class="data.DateBean" />
<jsp:setProperty name="datebeann" property="mydate" value="C:\\tomcathome\\anotherFile.jsp" />
<jsp:getProperty name="datebeann" property="mydate" />
Attempt in Java Bean:
private String mydate = "";
private String filename = "";
public DateBean()
{
this.mydate = fetchDate(filename);
}
public String fetchDate(String jspPath)
{
File jspFile = new File(jspPath);
Date lastModified = new Date(jspFile.lastModified());
SimpleDateFormat fmt = new SimpleDateFormat("EEEE, MMMM dd, yyyy, h:mm a(zz)");
return fmt.format(lastModified);
}
# 2
The initial problem: You shouldn't ever be constructing an instance of a Servlet yourself. The null pointer exception would have come from the call to getServletContext(), which was probably null.
Servlets are meant to be instantiated by the container, which then invokes methods according to the Servlet life-cycle.
The method that you have "fetch Date" may as well be a static method in a helper class - it has no purpose being in a servlet.
The change required to make it work: pass it a ServletContext as well as a Request object (or alternatively get the ServletContext from request via request.getSession().getServletContext()
package helper
public class HelperFunctions;
public static String fetchDate(HttpServletRequest request; ServletContext application)
{
String jspPath = application.getRealPath(request.getServletPath());
File jspFile = new File(jspPath);
Date lastModified = new Date(jspFile.lastModified());
SimpleDateFormat fmt = new SimpleDateFormat("EEEE, MMMM dd, yyyy, h:mm a(zz)");
return fmt.format(lastModified);
}
JSP:
String myDate = HelperFunctions.fetchDate(request, application);
You may want to name your method a bit better eg fetchJSPFileEditDate() or some such :-)
Also, rather than having the formatting done in that method, I would probably have it actually return a date, and then format it appropriately in the JSP with a JSTL <fmt:formatDate> tag, but thats just a slight difference.
Cheers,
evnafets
# 3
> The initial problem: You shouldn't ever be
> constructing an instance of a Servlet yourself. The
> null pointer exception would have come from the call
> to getServletContext(), which was probably null.
Thanks, I picked up alot from your post.To verifiy, This part here is constructing a Servlet instance/object (using the new keyword) and that is never to be done in a JSP?
String myDate = new DateServlet().fetchDate(request);
> The method that you have "fetch Date" may as well be
> a static method in a helper class - it has no purpose
> being in a servlet.
> The change required to make it work: pass it a
> ServletContext as well as a Request object (or
> alternatively get the ServletContext from request via
> request.getSession().getServletContext()
I can use a static method because that does not create a new Servlet instance because static methods dont create object instances?
String myDate = HelperFunctions.fetchDate(request, application);
# 4
Yep, thats the basics of my advice here.
>This part here is constructing a Servlet instance/object (using the new keyword)
>and that is never to be done in a JSP?
That shouldn't be done full stop. The container will create and prepare the Servlet instances for you, and call the service() method at the appropriate times. That doesn't stop you creating your own beans and instantiating them, but be aware that just because you can call new Servlet() doesn't mean that you should.
The helper method does not HAVE to be static, however it doesn't have any benefit being non static, as it is entirely selfcontained (now), and doesn't depend upon any particular instance of the class.
Cheers,
evnafets
# 5
Thanks for all your support and time!