2015-01-31 5 views
0

음, 이미 다른 게시물을 읽고 내 문제를 해결할 수 없습니다. 내 ManagedBean에 dataTable (JSF) 바인딩이 있습니다. 내가 선택한 요소의 목록을 가지고 있고이 요소를 제거 싶어, 참조 :목록에서 요소를 제거하려고 할 때 ConcurrentModificationException이 발생했습니다.

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

을 I가 시도 1

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) 

편집 : "목록"에서 제거하려고 할 때 데이터베이스하지만 난 오류가 발생했습니다 요소를 제거하도록 반복자를 두는 my 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(); 
+0

이는 어쨌든 JSF 관련되지 않는다. Java SE입니다. 해당 컬렉션을 반복하는 동안 컬렉션의 요소를 제거 할 수 없습니다. 기능은 대신 iterator의 remove 메소드에 의해 통합됩니다. 해답의 제시대로 이터레이터의 remove 메소드를 사용해야한다. – Tiny

+0

"* 반복 중에 컬렉션을 수정하는 유일한 방법은 Iterator.remove이며, 반복이 진행되는 동안 기본 컬렉션이 다른 방법으로 수정되면 비헤이비어가 지정되지 않습니다. *"http://docs.oracle .com/javase/tutorial/collections/interfaces/collection.html – Tiny

답변

3

,

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

당신은 다른 유사한 게시물을 읽은 경우 는, 당신은 당신은 단지 으로 반복자를 삭제할 수 있다는 것을 알고 이미합니다, 당신은 일을하지 않을 것을, 이것은 정확히 당신이해야하는 일입니다.

예는

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

하지만 목록에서이 요소를 제거해야합니다. 어떻게 해결할 수 있습니까? – Shelly

+0

@ user2776409 : 반복자를 사용하여 목록을 반복합니다. –

+0

그래서 내부 메서드 "deletar()"대신 "beans.remove (b)"를 사용해야합니다. beans.iterator를 또 만들어야하고 요소를 제거해야합니까? 맞았 어? – Shelly

관련 문제