aspose file tools
The moose likes Java in General and the fly likes .getResourceAsStream null Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark ".getResourceAsStream null" Watch ".getResourceAsStream null" New topic
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
    
    1

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: .getResourceAsStream null
 
Similar Threads
Struts2 + Hibernate
Struts2 cookies
Page navigation using struts2
Struts2 Internationalization
How to change the key=value pair of properties file dynamically