| 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: 56224
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 56224
|
|
|
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.
|
 |
 |
|
|
subject: request.getSession().getServletContext().getRealPath("/") with java.lang.NullPointerException
|
|
|