• 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

Trying to iterate an ArrayList of Beans

 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an ArrayList in which each element of the ArrayList is an instance of my Bean. I'm trying to use the <c:forEach> tag to iterate through it in my jsp but I get this error...

[11/29/07 16:09:31:794 EST] 0000002f SystemErr R javax.servlet.ServletException: The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.

Here is what my code looks like...



my list is called "list". am I missing something with the naming conventions? orderNumber is the only unique column in the arraylist... am I not referencing it right? I'm kind of confused...but i thought this would work...
[ November 30, 2007: Message edited by: Bear Bibeault ]
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured it out. I needed to change the items="${list.orderNumber}" to items="${row.orderNumber}". Now I get no errors. And I get no values printed either. I need to check if it is if my beans are empty or if my EL isn't working...
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<c:forEach var="list" items="${list.orderNumber}">

If you want to iterate over the list, why are you specifying a property of the list in the items attribute?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, i realized that and changed it to just be items="${row}" but that still hasn't produced results... I'm then using the EL like this...

${row.orderNumber}

I'm just not positive that will give me what I want, but I think it should.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I just dump the contents of my arraylist to the page? I know I would have to iterate over it, but is there a way to dump all the data in the rows without calling each element of the bean?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does "row" come from. You stated the that scopde variable for the list was named "list".
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bear, sorry about the incomplete information. My bean is called row. I do


Then I do


^^^^^I do this for each element I will need in my jsp.

Then I do list.add(row);

and finally I request.setAttribute("list",list);


Should I not be using "row" anywhere in my <c:forEach>?
This is how I have it now...

 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WHEEEEE, I got it working. Only one small problem to overcome now. I need to clear the arraylist. If I run it, and then close my browser, an then come back my server is remembering the old arraylist. How do I go about making sure its empty?

I'm gonna check the methods for arraylist and see if that have a way to wipe it clean...i'm sure they do...

thanks guys
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and finally I request.setAttribute("list",list);

The name of the Java variable is moot. You've created a scoped variable named "list".
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bryce Martin:
If I run it, and then close my browser, an then come back my server is remembering the old arraylist.

It can;t. You've assigned it to local variables and to a request scoped variable that goes out of scope when the response is returned to the browser. It;s much more likely that the page is being cached by the browser.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I keep it from caching then? I can't have that.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the Servlets FAQ.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It didn't work. I don't think its a caching issue. I create the list every time I run the servlet. But if the List already exists in memory it won't recreate it right? The reason why I think this is because I restarted the server and the array list was clear and worked as it was supposed to.
[ November 30, 2007: Message edited by: Bryce Martin ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bryce Martin:
I create the list every time I run the servlet. But if the List already exists in memory it won't recreate it right?

Of course it will. Java is going to do exactly what you tell it to do. If you tell it to create a new list, it's not going to search around for an existing one -- it'll create the new list.

Perhaps it's time to open a new topic in the Servlets forum for this issue showing your list creation code.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic