File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Context Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet Context Listener" Watch "Servlet Context Listener" New topic
Author

Servlet Context Listener

stefania ferrarelli
Greenhorn

Joined: Jul 26, 2009
Posts: 29
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2268
    
    3

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.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25

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

Joined: Dec 28, 2007
Posts: 2268
    
    3

Frits Walraven wrote:Not exactly, it would return null


No champ, I mean something like this:

Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1733
    
  25


No champ, I mean something like this:

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


Oh yeah, sure

Regards,
Frits
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
stefania ferrarelli
Greenhorn

Joined: Jul 26, 2009
Posts: 29
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Context Listener