| Author |
.getResourceAsStream null
|
debraj mallick
Ranch Hand
Joined: Mar 08, 2011
Posts: 188
|
|
hi all,
i am using the code below and i am getting "java.lang.NullPointerException"
i also get this exception when i am using getClassLoader()
any help would be appreciated.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
|
Are you trying to load the resource from a jar? In that case, the String should probably be something like "/WEB-INF/classes/global.properties". If the resource is not in a jar, or in a location that is relative to the class file, you should probably use the Path class to get access to the resource.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
If you're going to use absolute paths like this, FileInputStream is the class you'll need.
Note that within web containers you shouldn't use absolute files like this. You should take a path relative to the root of the web application ("/WEB-INF/...."), then use getServletContext().getRealPath(...) to turn that relative path into an absolute path. In JSP pages you replace getServletContext() with application.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
And if you need to use getResourceAsStream(...), it should take a path relative to the current class or to the class path. As the classes folder will be on the class path for web containers, the path should be "/global.properties" (the / makes it relative to the class path instead of the current class).
|
 |
debraj mallick
Ranch Hand
Joined: Mar 08, 2011
Posts: 188
|
|
thanks, Rob Spoor. it works
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
You're welcome
|
 |
 |
|
|
subject: .getResourceAsStream null
|
|
|