Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF java.util. Concurrent ModificationException  RSS feed

 
Dennis Black
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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 ]
 
Peter Goldstein
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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">
<hutputText value="Product"/>
</f:facet>
<hutputText value="#{item.productName}" rendered="#{item.quantity >= 1}" />
</h:column>
<h:column>
<f:facet name="header">
<hutputText value="Price"/>
</f:facet>
<hutputText value="#{item.unitPrice}" rendered="#{item.quantity >= 1}"/>
</h:column>
<h:column>
<f:facet name="header">
<hutputText value="quantity"/>
</f:facet>
<h:inputText value="#{item.quantity}" rendered="#{item.quantity >= 1}" required="true" />
</h:column>
<h:column>
<f:facet name="header">
<hutputText value="Total"/>
</f:facet>
<hutputText value="#{item.totalPrice}" rendered="#{item.quantity >= 1}" />
</h:column>
</h:dataTable>
 
Peter Goldstein
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Peter, Thanks much. Total noobar mistake on my part.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!