Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Si Liu
Greenhorn
Posts: 2
  • Likes 1
  • 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 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
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Si Liu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sh Levinson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The following code can be used instead:

this.getClass().getResource("/").getPath();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic