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();
2
  • 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. Commented Feb 1, 2015 at 4: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." docs.oracle.com/javase/tutorial/collections/interfaces/… Commented Feb 1, 2015 at 4:13

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

7 Comments

But i need remove this element from my list, how can i solve that ?
@user2776409: use an Iterator to iterate through the list, of course.
So, inside method "deletar()" instead use "beans.remove(b)" i should make another beans.iterator and remove the element ? it's correct ?
@user2776409: no. You MUST call remove() on the Iterator object (as I've been saying I thought). Not on beans, on it. Again, if you've done the searching that you have stated you've done, this should be obvious, because they all will tell you exactly the same thing.
@user2776409: now you're using a different iterator to remove from the one you're using to iterate through the list. You must use the exact same iterator, not two different ones.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.