aspose file tools*
The moose likes Servlets and the fly likes request.getSession().getServletContext().getRealPath( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException" Watch "request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException" New topic
Author

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

Deepu Surendran
Greenhorn

Joined: Feb 07, 2013
Posts: 2
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

Debugging 101: if you are getting an NPE, then something in the statement is returning null. Try each one to see which it is.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prashanth Akunuri
Greenhorn

Joined: May 21, 2012
Posts: 1
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

Joined: Feb 06, 2012
Posts: 223

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:



OCPJP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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
 
subject: request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException