0

Well, i already read the others posts and can't solve my problem. I have a dataTable (JSF) binding in my ManagedBean. I have a list of selected elements and i wanna remove this elements, see:

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();
           }
           }
       }
    }
    }

My method above call another method named 'deletar()', see:

 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());
    }
    }

The Bean is deleted from Database but when try remove from "List" i got error:

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)

EDit 1

I tried to edit my deletar() method putting a iterator to remove element but didn't work, the error continue.

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();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shelly
  • 1,035
  • 5
  • 27
  • 51
  • This is not related to JSF anyway. It is Java SE. You cannot remove elements of a collection while iterating over that collection. The functionality is incorporated by iterator's remove method instead. You are supposed to use iterator's remove method in this case as the answer suggests. – Tiny Feb 01 '15 at 04:12
  • "*Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.*" http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html – Tiny Feb 01 '15 at 04:13

1 Answers1

3

You state,

Well, i already read the others posts and can't solve my problem.

If you've read the other similar posts, then you should already know that you can only delete with the iterator, something you're not doing, and this is exactly what you must do.

e.g.,

public void deltar(Iterator<Bean> it, Bean bean) {
   // ..... 
   it.remove();
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373