Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading file in servlet context listener

 
Ankita Vashist
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to read text file from servlet context listener.
here is the following code :

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

"Unable" means what? Which lines of code are executed, and which aren't? At which point does what actually happens start to deviate from what you expected to happen?

Is the "leagues-file" an absolute path? Relative paths do not work in a web app.

Is there anything on the log file? If so, what?
 
Ankita Vashist
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have defined the path of league file in Web-INF. the log file contains data of all the leagues. Whenever i execute the project, it gives null pointer exception error when list of all leagues is clicked.
I want my listener to read from the text file and display it to user when user clicks list of all leagues.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have defined the path of league file in Web-INF.

So is it an absolute path? If you print the leaguesFile variable to the log, does it have the correct absolute path?

the log file contains data of all the leagues.

So there are no error messages in the log at all?

Whenever i execute the project, it gives null pointer exception error when list of all leagues is clicked.

Earlier you said "I am unable to read text file from servlet context listener. " - does that mean there is no error/exception in the context listener, and you are merely suspecting that it does not work correctly? And the exception is actually in some other code?
 
Ankita Vashist
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are the files of the project with the different file names but the same problem.



List of all leagues : this is a servlet which get the attribute from context to display the text file data




 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what are the answers to my questions? You need to help us help you.
 
Ankita Vashist
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this is an absolute path.
and there is no error in the log.
there is a null pointer exception whenever i try to execute the code. List of league is unable to take the attributes from context listener and giving null pointer exception.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your ServletContextListener you go to a lot of trouble to create a list of DVD objects, but then you ignore that list and put a completely different object into the servlet context.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this line.


this is getting an context parameter with name "library.txt". which actually does not exists in the web.xml and which also return a null value.

try using "library_file" instead of "library.txt" in context.getInitParameter()
 
Ankita Vashist
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:Check this line.


this is getting an context parameter with name "library.txt". which actually does not exists in the web.xml and which also return a null value.

try using "library_file" instead of "library.txt" in context.getInitParameter()

I tried it , again getting the same error.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you by now fixed the problem Paul pointed out?

How many items are actually put into the list?
 
Ankita Vashist
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! i have corrected my code . but i am getting the same error. There are 5 items in the list.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... you have verified that the object you're putting into the context in the listener is a list of 5 elements, and the object you're getting out of the context in the servlet is not a list of 5 elements? Note that I'm not asking whether you got an exception, I'm asking how you have verified that the correct object is put into the context, and the wrong object (or no object) is retrieved from the context. And by "verify" I do not mean "look at the code and think to yourself "yes, that's what should be happening", I mean "actually verify".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!