• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Context Listener

 
stefania ferrarelli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
I try to read from a file set in web.xml as a context-param


and to store into the Listener the list contained in the file as a context attribute. I get a runtime exception when I try to read the .txt file


on the line is = context.getResourceAsStream(libreria);
I get a NullPointerException.
Could you give me a suggestion? I'm not sure that the path in param-value is right, it means I'm not sure I built the package in the right location because on netbeans I obtain a package inside source packages.Do you think I need to create programmatically the File and the dir?
Thanks
Stefania
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Stefania!

Well, I don't think the context is null because, if it was, then you would get a NullPointerException in the context.getInitParameter("library-file"); instruction. Also, the API says that, if the resource can't be found, then the getResourceAsStream method only returns null.

Can you please show us the stack trace? Also, as Bear already mentioned, please do not forget to use code tags when posting code to the forums.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2420
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefania,

if it was, then you would get a NullPointerException in the context.getInitParameter("library-file");

Not exactly, it would return null

The file you are looking for is not in the directory /WEB_INF/data (or it has another name....).

Check the structure of your war-file, or create the .war file manually (see war by hand)

Regards,
Frits
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frits Walraven wrote:Not exactly, it would return null


No champ, I mean something like this:

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2420
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No champ, I mean something like this:

ServletContext context = null;
context.getInitParameter("library-file");


Oh yeah, sure

Regards,
Frits
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stefania ferrarelli wrote:on the line is = context.getResourceAsStream(libreria);
I get a NullPointerException.

Are you the exception is at this line. I don't think the context would be null but if the /WEB-INF/data/my-library.txt doesn't exist, then context.getResourceAsStream will return null which can result in a NullPointerException...
 
stefania ferrarelli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all!!
I tryed to put the .txt file directly inside the root of the webapp

and now everything works.
I still cannot understand why creating a package data by netbeans and putting a file inside it didn't work. Do you think it is correct to put a file directly inside Web Pages? If I create a directory with

why I cannot find the package inside netbeans? Can I assume a package is a directory?

Sorry for not using tag code in the last post, I didn't know about it!!

You all helped me a lot!!
Stefania
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic