尝试从列表中删除元素时出现ConcurrentModificationException

时间:2015-01-31 20:45:12

标签: java

好吧,我已经阅读了其他帖子,无法解决我的问题。我的ManagedBean中有一个dataTable(JSF)绑定。我有一个选定元素的列表,我想删除这些元素,请参阅:

public void removeSelected() {

    for (Map.Entry<Integer, Boolean> entry : registrosSelecionados.entrySet()){
       if (entry.getValue() == true){
           int id = entry.getKey();
           Iterator<Bean> it = beans.iterator();
           while(it.hasNext()){
           Bean b = it.next();
           if (b.getId().equals(id)){
               setBean(b);
               deletar();
           }
           }
       }
    }
    }

我上面的方法调用另一个名为'deletar()'的方法,参见:

 public void deletar() {
    try {


        //Se o bean for nulo então capturamos o bean selecionado no DataTable, se este existir
        if (bean == null){
        if (dataTable == null){
            throw new RuntimeException("O bean é nulo e não há dataTable vinculado ao ManagedBean. A deleção será abortada");
        }
        bean = (Bean) dataTable.getRowData();
        }

        beforeRemove();
        getBoPadrao().delete((AbstractBean) bean);
        addInfoMessage("Registro deletado com sucesso");
        beans.remove(bean);
        bean = null;
        afterRemove();
    } catch (BOException e) {
        addErrorMessage(e.getMessage());
        FacesContext.getCurrentInstance().validationFailed();
    } catch (Exception e) {
        e.printStackTrace();
        logger.error((new StringBuilder()).append("Erro ao deletar: ")
            .append(e.getMessage()).toString());
        FacesContext.getCurrentInstance().validationFailed();
        addErrorMessage((new StringBuilder()).append("Erro ao deletar. ")
            .append(e.getMessage()).toString());
    }
    }

Bean从数据库中删除但是当尝试从“列表”中删除时出现错误:

Jan 31, 2015 5:38:32 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
Grave: java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at br.com.jwebbuild.mb.BasicCrudMBImpl.removeSelected(BasicCrudMBImpl.java:226)

编辑1

我尝试编辑我的deletar()方法,将迭代器放到删除元素但不起作用,错误继续。

public void deletar() {
    try {


        //Se o bean for nulo então capturamos o bean selecionado no DataTable, se este existir
        if (bean == null){
        if (dataTable == null){
            throw new RuntimeException("O bean é nulo e não há dataTable vinculado ao ManagedBean. A deleção será abortada");
        }
        bean = (Bean) dataTable.getRowData();
        }

        beforeRemove();
        getBoPadrao().delete((AbstractBean) bean);
        addInfoMessage("Registro deletado com sucesso");

        Iterator<Bean> it = beans.iterator();
        while (it.hasNext()) {
        Bean b = it.next();
        if (b.equals(bean)) {
            it.remove();
        }
        }

        bean = null;
        afterRemove();

1 个答案:

答案 0 :(得分:3)

你说,

  

好吧,我已经阅读了其他帖子,无法解决我的问题。

如果您已阅读其他类似帖子,那么您应该已经知道只能删除 迭代器,这是您不做的事情,这正是您必须要做的事情。

如,

public void deltar(Iterator<Bean> it, Bean bean) {
   // ..... 
   it.remove();
}