Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException

 
Deepu Surendran
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I am working on a java web project using struts2. My developing environment is Eclipse+ tomcat on windows

I encountered a problem when I would like to get the path of current project.

This is the code I am using to get the path.
String filePath = request.getSession().getServletContext().getRealPath("/");

But it always returns java.lang.NullPointerException

Hope any experts could give me some help.



Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debugging 101: if you are getting an NPE, then something in the statement is returning null. Try each one to see which it is.
 
Prashanth Akunuri
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not an expert, but try:

request.getServletContext().getRealPath("");

Also refer this article:

http://www.mkyong.com/javaee/the-method-getrealpathstring-from-the-type-servletrequest-is-deprecated/
 
Ishan Pandya
Ranch Hand
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is any need for getting the session (Which may be not initialized and which may result in NPE) for obtaining the context path.
Context path is common not a session specific.

You may use:



Even more better:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:
Even more better:



That's for a JSP. As this was posted in the Servlets forum, it is assumed that the OP is asking about servlets.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic