• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JSF java.util. Concurrent ModificationException

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I know what my problem is, I just don't know how to get around it.

I have a shopcart that allows the user to update quantity, pretty straightfoward.

<h:form >
<h ataTable var="item" value="#{shopCartBean.orderItems}">
<h:column>
<f:facet name="header">
<h utputText value="Product"/>
</f:facet>
<h utputText value="#{item.productName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h utputText value="Price"/>
</f:facet>
<h utputText value="#{item.unitPrice}"/>
</h:column>
<h:column>
<f:facet name="header">
<h utputText value="quantity"/>
</f:facet>
<h:inputText value="#{item.quantity}" required="true" />
</h:column>
<h:column>
<f:facet name="header">
<h utputText value="Total"/>
</f:facet>
<h utputText value="#{item.totalPrice}" />
</h:column>
</h ataTable>
<h:commandButton value="update" action="#{shopCartBean.updateCart}" />
</h:form>

Problem is, if they update the quantity to zero, I want to take that item out of the cart. So I do it like this...

public String updateCart() {
Iterator it = orderItems.iterator();
while (it.hasNext()) {
OrderItem oi = (OrderItem) it.next();
if (oi.getQuantity() == 0)
orderItems.remove(oi);
}
return "review";
}

Which throws a java. util. Concurrent ModificationException I gather because JSF is messing with the list while I'm removing it. I made orderItems a synchronizedList, didn't help at all.

Any ideas for a work around?

Thanks,
Dennis
[ February 27, 2006: Message edited by: Dennis Black ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you make a new List that is a copy of the old one, remove the item from the copy, and set the new List in the bean? I think that should work as a workaround.
 
Dennis Black
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter I thought you had the solution for sure. I was even doing the guinness guys "Brilliant" in my head.

But it didn't work Same error.


public String updateCart() {
List newOrderItems = new ArrayList(this.orderItems);
Iterator it = newOrderItems.iterator();
while (it.hasNext()) {
OrderItem i = (OrderItem) it.next();
if (i.getQuantity() < 1)
newOrderItems.remove(i);
}
this.orderItems = newOrderItems;
return "review";
}
 
Dennis Black
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well here is my solution. I wish I could say it's the cheesiest thing I've done since I started using JSF, but it's not even in the top 10.

<h:dataTable var="item" value="#{shopCartBean.orderItems}">
<h:column>
<f:facet name="header">
<h:outputText value="Product"/>
</f:facet>
<h:outputText value="#{item.productName}" rendered="#{item.quantity >= 1}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Price"/>
</f:facet>
<h:outputText value="#{item.unitPrice}" rendered="#{item.quantity >= 1}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="quantity"/>
</f:facet>
<h:inputText value="#{item.quantity}" rendered="#{item.quantity >= 1}" required="true" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Total"/>
</f:facet>
<h:outputText value="#{item.totalPrice}" rendered="#{item.quantity >= 1}" />
</h:column>
</h:dataTable>
 
Peter Goldstein
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dennis, I didn't look closely enough at your original post. I don't think the problem is that JSF is doing something with the list, it's that you've got an open Iterator and are using the Collection methods rather than Iterator methods to remove the item.

Try replacing otherItems.remove(o) with it.remove(o) in your original solution. That may very well solve your problem.

That would explain why you're encountering the same problem with the copied List, which the underlying code can't be manipulating.
 
Dennis Black
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter, Thanks much. Total noobar mistake on my part.
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic