File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Null pointer at request.getRequestDispatcher( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Null pointer at request.getRequestDispatcher("foo.jsp").forward(request, response)" Watch "Null pointer at request.getRequestDispatcher("foo.jsp").forward(request, response)" New topic
Author

Null pointer at request.getRequestDispatcher("foo.jsp").forward(request, response)

Tom Hilliard
Ranch Hand

Joined: Feb 03, 2012
Posts: 39

Hi all, first time posting here

I'm implementing a search function on my web system. I take an input from an HTML <input> on the index.jsp and set it in the request. I take this input in SearchPlace.java (servlet) and make an array list of my results to pass to the displayaccommodations.jsp. I also pass the search term from SearchPlace.java to displayaccommodations.jsp via the request.

My code for SearchPlace.java



and the output from the console:

03-Feb-2012 10:03:56 org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/AccommodationActivitySystem] has started
03-Feb-2012 10:03:56 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/AccommodationActivitySystem] registered the JDBC driver [org.postgresql.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
03-Feb-2012 10:03:56 org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/AccommodationActivitySystem] has started
PLACE REMOVED
SEARCH TERM IS: 'norfolk'
SQL(place) IS: SELECT acc_name, acc_briefdesc, sleeps, price, acc_city, acc_county, acc_id FROM "AAProject"."accommodation" WHERE acc_city ILIKE 'norfolk' OR acc_county ILIKE 'norfolk' ORDER BY acc_city ASC;
RS DONE
ROW COUNT IS: 2
AL DONE
SEARCH TERM SET - norfolk
AL ACC SET - [Chameleon , Chameleon House, a wonderful place to stay on the Norfolk/Suffolk , 4, 85.00, Harleston, Norfolk, 14]
AL ACC SET - [The Love , Love Shack lovely place to , 2, 75.00, Norwich, Norfolk, 7]
Error is: org.apache.jasper.JasperException: java.lang.NullPointerException

and the exception -

03-Feb-2012 10:04:05 org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.displayaccommodations_jsp._jspService(displayaccommodations_jsp.java:170)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at servlets.SearchPlace.processRequest(SearchPlace.java:73)
at servlets.SearchPlace.doPost(SearchPlace.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:662)


So as far as my problem! I cannot work out what is breaking!

NB: I did have this feature working well until I started playing around with sessions for user loggin, but as far as I'm aware the session only comes into play after logging in, where the feature still doesn't work!


Regards

TH
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
You could figure out the code throwing NPE by looking at the .java file generated for the jsp throwing the error. In case you are using tomcat, its placed in the work directory.


Thanks and Regards
Tom Hilliard
Ranch Hand

Joined: Feb 03, 2012
Posts: 39

Cheers for the fast reply. I'm not sure what you mean, nor do I know what .java file you are talking about. I am using Tomcat though.

Regards

TH
Tom Hilliard
Ranch Hand

Joined: Feb 03, 2012
Posts: 39

So is it the SearchPlace.java or displayaccommodations.jsp throwing the error?

Is it breaking at the dispatch because of an error on the displayaccommodations.jsp it dispatches to?

Regards

TH
Tom Hilliard
Ranch Hand

Joined: Feb 03, 2012
Posts: 39

if i change my code to this:



notice I have changed the way of doing the RequestDispatcher

it now breaks at the initialisation of RequestDispatcher = rd; (line 69)

I have no idea what or how to fix this

Regards TH
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

What do you mean "it breaks" at that line? Please be precise. What is the exact error and stack trace?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tom Hilliard
Ranch Hand

Joined: Feb 03, 2012
Posts: 39



By breaks I mean, a null pointer and netbeans points to line 72 in the Tomcat 6.0 Log which follows:

03-Feb-2012 15:57:09 org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.displayaccommodations_jsp._jspService(displayaccommodations_jsp.java:170)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at servlets.SearchPlace.processRequest(SearchPlace.java:72)
at servlets.SearchPlace.doPost(SearchPlace.java:108)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:662)


Here is the console output containing the stack trace (hopefully!). I used ex.printStackTrace(); to get the stack trace

PLACE REMOVED
SEARCH TERM IS: 'norfolk'
SQL(place) IS: SELECT acc_name, acc_briefdesc, sleeps, price, acc_city, acc_county, acc_id FROM "AAProject"."accommodation" WHERE acc_city ILIKE 'norfolk' OR acc_county ILIKE 'norfolk' ORDER BY acc_city ASC;
RS DONE
ROW COUNT IS: 2
AL DONE
SEARCH TERM SET - norfolk
AL ACC SET - [Chameleon House, Chameleon House, a wonderful place to stay on the Norfolk/Suffolk boarder. , 4, 85.00, Harleston, Norfolk, 14]
AL ACC SET - [The Love , Love Shack lovely place to , 2, 75.00, Norwich, Norfolk, 7]
Error is: org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.JasperException: java.lang.NullPointerException
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at servlets.SearchPlace.processRequest(SearchPlace.java:72)
at servlets.SearchPlace.doPost(SearchPlace.java:108)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
at org.apache.jsp.displayaccommodations_jsp._jspService(displayaccommodations_jsp.java:170)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
... 25 more

And thanks for that suggestion (printing stack trace) as now I can see theres an error on the displayaccommodations.jsp im going to after SearchPlace.java

So now I'll include the code for displayaccommodations.jsp



but noticing this hasnt helped, I can't find (what is probably an obvious) error on displayaccommodations.jsp. Its still not working!

(Scriptlets in jsp... I know its not perfect but neither am I )

Regards

TH
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

The stack trace line:
at org.apache.jsp.displayaccommodations_jsp._jspService(displayaccommodations_jsp.java:170)
indicates that the exception is occurring inside the JSP.

You are still putting Java code in scriptlets in a JSP 10 years after they have been discredited and replaced with JSTL and EL?

Problems like this are just one of the many reasons that scriptlets are passé. Step one is to refactor your code to remove all Java code from the JSP and use modern techniques and proper practices.

If you are fairly novice to JSP, I recommend reading these articles:
  • The Secret Life of JSPs
  • The Front Man

  • Tom Hilliard
    Ranch Hand

    Joined: Feb 03, 2012
    Posts: 39

    1. I know the error exists in displayaccommodations.jsp
    2. I was taught by my uni to code the way I do!

    I'm p*ssed because they are teaching me old concepts... They have not taught HTML 5, CSS 3 or any newer technologies. Thats by the by.

    Also, I'm not prepared to change the way I've written code, I'm not a programmer nor will I ever be. If this means you won't/can't help then fair enough, I'll go look for help elsewhere.

    Like I said before, I had this feature working before I started exploring sessions etc. so the issue is not with java scriptlets in a jsp.

    So now the error location has been determined how do I go about discovering the actual error, is there anything like the StackTrace that can help?

    Regards

    TH
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61106
        
      66

    The stack trace shows which line is failing in the Java code generated for the JSP. Where that Java file gets stored depends upon the container that you are using. But finding the file and locating the failing line seems to be your next step.
    Tom Hilliard
    Ranch Hand

    Joined: Feb 03, 2012
    Posts: 39

    Cheers
    And sorry but, by container you mean what?

    Regards TH
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61106
        
      66

    Tomcat, Webshpere, Weblogic, Glassfish, etc...
    Tom Hilliard
    Ranch Hand

    Joined: Feb 03, 2012
    Posts: 39

    Tomcat 6.0 can you tell me where I find this file and what it is likely to be called, I think it was mentioned earlier.

    Regards
    TH
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    The stack trace tells you that the name of the file you're looking for is "displayaccommodations_jsp.java". Line 170 in that file is the one which threw the exception.

    Whatever operating system you are using will have a feature which allows you to search the whole file system for a file with a particular name.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61106
        
      66

    Tomcat stores the generated files under its own work folder. It'll likely be nested fairly deeply, so you'll need to poke around or do a file search for that specific file.
     
     
    subject: Null pointer at request.getRequestDispatcher("foo.jsp").forward(request, response)