wood burning stoves 2.0*
The moose likes Servlets and the fly likes NullPointer Exception for ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "NullPointer Exception for ServletContext" Watch "NullPointer Exception for ServletContext" New topic
Author

NullPointer Exception for ServletContext

bindu sadanandan
Greenhorn

Joined: Jul 31, 2012
Posts: 6



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]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Jul 31, 2012
Posts: 6
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

Joined: Jul 31, 2012
Posts: 6
Can anyone figure out why the ListLeaguesServlet.java is not*listing the contents that I enter in the context parameter file leagues.txt ?
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

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

Joined: Jul 31, 2012
Posts: 6
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?......











deepu sharma
Greenhorn

Joined: Jul 28, 2009
Posts: 8
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.............................
Ankush Puri
Greenhorn

Joined: Jul 16, 2010
Posts: 6

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointer Exception for ServletContext
 
Similar Threads
autowiring problem when deploying on Jboss through eclipse
servlet context listener problem
autowiring problem when deploying on Jboss through eclipse
compilation error of servlet
failure to activate a Web application with database