File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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

Deepu Surendran

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.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

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] [About Bear] [Books by Bear]
Prashanth Akunuri

Joined: May 21, 2012
Posts: 1
I am not an expert, but try:


Also refer this article:
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 225

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

Joined: Jan 10, 2002
Posts: 63865

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:
subject: request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException
jQuery in Action, 3rd edition