• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointer Exception for ServletContext

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I get a NullPointer Exception .... the leagues-file is not getting populated from the context....not sure why....can anyone check to try any modifications..?

The ServletContextListener is as follows:
============================


===============================================================


My web.xml file is as follows :
======================================



Awaiting reply...

[Added code tags - see UseCodeTags for details]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bindu. Welcome to the Ranch!

bindu sadanandan wrote:I get a NullPointer Exception .... the leagues-file is not getting populated from the context....not sure why....can anyone check to try any modifications..?

Awaiting reply...




Where do you get it? The error message will tell you exactly which line it happened on, and you're likely to get a useful reply if you tell us.

(You're also more likely to get one in the Servlets forum, so I've moved the post there).
 
bindu sadanandan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
bindu sadanandan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone figure out why the ListLeaguesServlet.java is not*listing the contents that I enter in the context parameter file leagues.txt ?
 
Ranch Hand
Posts: 136
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since leagueList has a list of values try this getAttributeNames() instead of getAttribute() it comes under java.util.Enumeration Now iterate it you will get the values
 
bindu sadanandan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to use Enumeration in order to retrieve more than one context-parameters right ? But I only have one context parameter

Context Parameter

Parameter Name Parameter Value
leagues-file /WEB-INF/data/leagues.txt

The league.txt has single line with multiple columns separated by tab.For this I have used below code in order to retrieve multiple values

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);

here I have only one context-parameter leagues-file pointing to leagues.txt...

I have multiple entries in leagues.txt ...

The leaguelist here i passed as context.setAttribute("leagueList", leagueList); line 52 ( I passed a list name leagueList (right ) as object and gave it a random name "leagueList"(left) which is String )

context.setAttribute( String string , Object object )



In my listleaguesServlet.java when I get the attribute from ServletContext still there is only one attribute that I set leagueList(left) which is String ( string can have multiple entries )and I have typecasted

leagueList = (List) context.getAttribute("leagueList"); line 27


I have used iterator to list the values of list leagueList (left) which i got after typecasting.


The below code is to get multiple context-parameters

String value=null;

Enumeration e=getServletContext().getInitParameterNames();
while(e.hasMoreElements())
{
value=getServletContext().getInitParameter((String) e.nextElement());
out.println(value + "<br>");
}

which will give /WEB-INF/data/leagues.txt as output


But I only have a single context parameter..........can you tell me where exactly you want me to use Enumeration?......











 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear bindu
please try to some comments............ and indents............ its very hard to read ................. this is not history right keep the things small and easily.............................
 
Greenhorn
Posts: 6
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code seems fine.....I think there is some problem with the following statement

String[] fields = record.split("\t");


Either your file is not properly delimited or there is some problem with "\t".....Please check...
 
reply
    Bookmark Topic Watch Topic
  • New Topic