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 request.getSession().getServletContext().getRealPath( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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

Si Liu
Greenhorn

Joined: Dec 23, 2012
Posts: 2
Dear all,

I am working on a java web project using struts2. My developing environment is Netbeans+ tomcat+ maven on Mac.

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

Besides I also have problem in getting property file inside my project. File can't be read.

I have tested my code on Windows and Linux. Both work fine. But only can't work on Mac.

So, I am wondering if there is some special configuration need to pay attention?

Hope any experts could give me some help.

Appreciate it a lot.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Si Liu wrote:String filePath = request.getSession().getServletContext().getRealPath("/");

Are you sure that it's this line? The only way that can NPE is if getSession() or setServletContext() returns null. Check each of these.

Do you have an init() method in the servlet? If so, show it to us.

Besides I also have problem ...

Please do not ask more than one question per topic. Start a new topic for a new question.

So, I am wondering if there is some special configuration need to pay attention?

No.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Si Liu
Greenhorn

Joined: Dec 23, 2012
Posts: 2
Hi Bear,

Thanks for your suggestions.

I tried : HttpSession session = request.getSession();

It gives the error:

java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class HttpSession
location: class perfsonar.fileupload.FileUploadAction

It seems because Netbeans is using two different sources of "class file" data such that it can resolve all the symbols
on a build, yet the runtime classpath and editor is using something different...

I found that the WAR file is not in the directory of tomcat/webapps but in the directory of the netbeans/target, when deploying the project...

Do you have any idea on this?

Thanks a lot~
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Ditch the IDE and deploy the app in Tomcat. I use an IDE for editing, but run the apps standalone in Tomcat. It gets rid of all the IDE nonsense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException
 
Similar Threads
how to load properties file
UnitTest in Spring
want to insert user-defined pass-phrase
Need help regarding Server's Real Path.
getRealPath returns null in Linux(Fedora)