Discussions

Web tier: servlets, JSP, Web frameworks: Using RequestDispatcher, NullPointerException

  1. Hi,
    I am using RequestDispatcher to forward data from servlet to JSP.
    [CODE]
    try{
    HttpSession session=request.getSession(true);
    session.setAttribute("LoginMessage",loginMessage);
    RequestDispatcher rd=request.getRequestDispatcher("../LoginMessage.jsp");
    rd.forward(request,response);
    }catch(Exception e){
    System.out.println("error2 in doGet "+e);
    e.printStackTrace();
    out.println("error2 in doGet");
    [/CODE]

    I am working in JBoss 3.2.1, When I run the servlet in browser I get this error:
    [CODE]
    21:02:02,013 INFO [STDOUT] error2 in doGet java.lang.NullPointerException
    21:02:02,013 ERROR [STDERR] java.lang.NullPointerException
    21:02:02,013 ERROR [STDERR] at au.com.tusc.servlet.MyServlet.doPost(MyServlet.java:69)
    21:02:02,013 ERROR [STDERR] at au.com.tusc.servlet.MyServlet.doGet(MyServlet.java:80)
    21:02:02,013 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    21:02:02,013 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    21:02:02,013 ERROR [STDERR] at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:360)
    21:02:02,013 ERROR [STDERR] at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:294)
    21:02:02,013 ERROR [STDERR] at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:558)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.HttpContext.handle(HttpContext.java:1714)
    21:02:02,013 ERROR [STDERR] at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:507)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.HttpContext.handle(HttpContext.java:1664)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.HttpServer.service(HttpServer.java:863)
    21:02:02,013 ERROR [STDERR] at org.jboss.jetty.Jetty.service(Jetty.java:460)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.HttpConnection.service(HttpConnection.java:775)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:939)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.HttpConnection.handle(HttpConnection.java:792)
    21:02:02,013 ERROR [STDERR] at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:201)
    21:02:02,013 ERROR [STDERR] at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:289)
    21:02:02,013 ERROR [STDERR] at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:455)

    [/CODE]

    What may be the problem? why is rd null? Please help.
    Thanks,
    Abhishek
  2. You sure it's rd that is null? (Check the line numbers in the stack trace against your source code or debug your doGet method).

    Note that null does not mean your jsp doesn't exist at the location specified. According to the Servlet API, getRequestDispatcher only returns null if the ServletContext cannot create a RequestDispatcher, which should not happen under normal conditions, I think.
  3. Yeap!! Your rd is null. Check your jsp path again. It’s not reachable.