File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Arraylist nullpointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Arraylist nullpointer exception" Watch "Arraylist nullpointer exception" New topic
Author

Arraylist nullpointer exception

Danne Girow
Greenhorn

Joined: Aug 24, 2006
Posts: 9
Can some one please tell me why I am getting a null pointer exception when I try to retrieve my arraylist elements. When I look at my print lines I see that the array does have values. Here is the code to build the array:

To get the array elements I call this class from my jsp page:

Here is the out put minus the systemOut:

Thanks for the help!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It looks like a very common beginner mistake, something everyone does once but few ever repeat: you've got at least two different variables named "Warehouses". One of them is local to the first method you show here; the other one is, I'm assuming, a member variable in the class. You initialize and store elements into the local one, which is discarded when that method returns. The class member one remains null, so when the second method you should above tries to read it, you get the NullPointerException.

To be explicit: this defines a variable and gives it a value:

ArrayList foo = new ArrayList();

while this assigns to a pre-existing variable:

foo = new ArrayList();

Your first method above should be doing the latter, not the former.

A couple more comments: following common Java coding conventions and naming variables and methods with names starting with lower-case letters makes your code easier for other Java programmers to read. And you can simplify your first method by getting rid of "i" and just using the one-argument "ArrayList.add()", which adds at the end.


[Jess in Action][AskingGoodQuestions]
Danne Girow
Greenhorn

Joined: Aug 24, 2006
Posts: 9
Thanks! That worked. I knew I was doing SOMETHING really lame and stupid I just couldn't fiuge out what.

Now I have a different issue...I am trying to use c:forEach to show what is in the whs array. The page throws a servlet error when I include the jstl tag at the top of the page. This is because I am including my page inside another page and that page has it defined. The problem is that when I don't include the jstl def the <c:forEach throws an error that says unknown tag. I don't get an error when running the page but all I do get is whs.warehouses instead of my list. Any ideas as to how to fix this. I have gotten the page to work by doing it the "old way" <% do{display the array items}while(stillmoredata) %> but I would like to keep my page standard with the other page which uses the tags.

Any thoughts on this?

Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As this is a JSP-specific issue, I'm going to move this thread to our JSP forum; go continue it there.
Danne Girow
Greenhorn

Joined: Aug 24, 2006
Posts: 9
I figured out what the problem was. My getwarehouses() method wasn't named correctly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arraylist nullpointer exception