• 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

Arraylist nullpointer exception

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Danne Girow
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out what the problem was. My getwarehouses() method wasn't named correctly.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic