I am trying to get content stored in global context parameter "leagues-file" that points to /WEB-INF/data/leagues.txt ..... the leagues.txt has the following contents
2003 Summer Summer Here
each corresponding to year,season and title ...... so I read the leagues.txt record by record by using ServletContext to get the file and read using BufferedReader...
ServletContext context = event.getServletContext();
String leaguesFile = context.getInitParameter("leagues-file");
List leagueList = new LinkedList();
InputStream is = null;
BufferedReader reader = null;
try {
is = context.getResourceAsStream(leaguesFile);
reader = new BufferedReader(new InputStreamReader(is));
String record;
// Read every record (one per line)
while ( (record = reader.readLine()) != null ) {
String[] fields = record.split("\t");
// Extract the data fields for the record
int year = Integer.parseInt(fields[0]);
String season = fields[1];
String title = fields[2];
// Add the new League item to the list
League item = new League(year, season, title);
leagueList.add(item);
}
context.setAttribute("leagueList", leagueList);
The year,season and title are passed to the League model constructor League item = new League(year, season, title); tat works fine.
And passed the leagueList as context attribute context.setAttribute("leagueList", leagueList);
But when I get the Attribute in my ListLeaguesServlet.java
ServletContext context=getServletContext();
leagueList = (List) context.getAttribute("leagueList");
the leagueList is showing null even though my leagues.txt file has contents... i chked null output by line 54
out.println(getServletContext().getAttribute("leagueList"));
======> prints as null in output.....
If I remove the if loop to see the Exception
// if ( leagueList !=null)
//{
Iterator items = leagueList.iterator();
out.println("<ul>");
while ( items.hasNext() ) {
League league = (League) items.next();
out.println(" <li>" + league.getTitle() + "</li>");
// }
The output will be :
null
Duke's Soccer League: List Leagues
The set of soccer leagues are:
java.lang.NullPointerException
============================
The first null was to chk the value of out.println(getServletContext().getAttribute("leagueList"));
Can anyone figure out why the ListLeaguesServlet.java is listing the contents that I enter in the context parameter file leagues.txt ?