I am having this issue with my Servlet, cant seem to understand. I
have the snippet of the servlet below whihch forwards the request to a
jsp based on what the method condition returns. The servlet however
does forward to the appropriate jsp but then gives a NUll pointer
exception.
Here is the servlet snippet
public void doPost (HttpServletRequest request, HttpServletResponse
response) throws ServletException, java.io.IOException {
ausername = request.getParameter("ausername");
apassword = request.getParameter("apassword");
try {
authenticate(ausername, apassword); //returns true if user is
valid and else false
if(aReturn == true) {
this.getServletContext().getRequestDispatcher("/gototrue.jsp").forward(request,
response);
}
else {
this.getServletContext().getRequestDispatcher("/gotofalse.jsp").forward(request,
response);
}
}
catch (ServletException se) {
}
Here is the error log i get:
<Error> <HTTP> <[WebAppServletContext(1531469,myapp,/
myapp)] Servlet failed with Exception
java.lang.NullPointerException
at csf.webcntrl.HTTPRMIAdapter.doPost(HTTPRMIAdapter.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:262)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:198)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
rvletContext.java:2637)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
pl.java:2359)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
Am i missing something, session , ServletContext() or something on
those lines.
Any help is appreciated
Before I speculate at what the problem is:
a) if(x == true) // yuk ! Better written as if(x)
b) Why are you writing your own security mechanism ? Are you encouraging
attacks against your application ? Why aren't you using J2EE authentication
mechanisms ?
Problem (speculation):
I'll bet my leftie that
this.getServletContext().getRequestDispatcher("/gototrue.jsp") is returning
null.
You can test this by:
RequestDispatcher rd =
this.getServletContext().getRequestDispatcher("/gototrue.jsp");
if(rd == null)
{
throw new ServletException("I broke it");
}
else
{
rd.forward(request, response);
}
Good luck !
--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
when u r comparing aReturn == true, aReturn should have some value that
has been returned by the authenticate method. You must do
aReturn = authenticate(ausername, apassword);
This should solve your NullPointerException.
hope it helps.
Let me know