• Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer exception while creating file from resources in managed bean classs  RSS feed

 
mostafa jamareh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to list files from resources folder like this:



but it give me a null pointer exception on fList
the directory of resources is :



How is this caused and how can I solve it?
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which line in the code, are you getting this exception at?
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem with this approach is that the official storage format for a J2EE webapp is a WAR File. Your resources, when in a WAR wouldn't be accessible as filesystem objects, thus any attempt to access them using java.io.File would fail. You could do so if the WAR had been "exploded" (unzipped), but exploded form is not part of the J2Ee standard, so doing so would violation the J2EE standard. Also, since webapp servers have no concept of "current directory", whether within a WAR or not, a relative pathname would be extremely risky to use, assuming it ever functioned at all.

The safer way to get at WAR resources is to use the HttpServletRequest getResource methods, which take as their pathnames an "absolute" resource location. Absolute relative to the root of the WAR, that is. So a resource path of "/WEB-inf/classes/log4j.xml" would return access to the log4j.xml "file" (see above) in a J2EE-compliant way. Note that the leading slash is mandatory, since it's an absolute path.

I'm not sure that theres a method that will enumerate WAR resources or not without reading the docs. Since a WAR is supposed to be read-only, you could always create a resource that contains the list of resources in it. If, on the other hand, the list of resources is subject to change, you might find it better to place them in a real directory somewhere outside both the WAR and the webapp server.
 
mostafa jamareh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:which line in the code, are you getting this exception at?

thanks rohit , on line 10
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!