Troubleshooting: ConcurrentModificationException when accessing MutableLiveData Object in Android


java.util.ConcurrentModificationException happened in MutableLiveData Object proxy design pattern

In android I have Class that named Exam , Exam Have a List of Question and each Question have a List of Answer , So I created ModelView Like :

private final MutableLiveData<Exam> liveExam;
    public MutableLiveData<Exam> getExam() {
        return liveExam;
    public void updateQuestion(int nextOrPrevious) throws Exception {
        int indexOfQuestion = questionPosition.getValue() + nextOrPrevious;
        if (indexOfQuestion >= 0 && (exam.getQuestions().size() >= (indexOfQuestion))) {
            mlExamQuestionNumber.postValue(TextUtil.toPersian(String.format(formattedQuestionNumberAndEndNumber, liveExam.getValue().getQuestions().get(indexOfQuestion).getIndex(), exam.getQuestions().size())));
        } else {
            throw new Exception("No more Question here");

everything work fine , but when I call :questionViewModel.getQuestionPosition().getValue().getQuestions()
if update answer like blow :

Iterator<Answer> iterator = exam.getQuestions().get(questionPosition)

while (iterator.hasNext()) {
         Answer answer =;
answer.setAnswer(answer.getId() == checkedId);
exam.getQuestions().get(questionPosition).getAnswers().set(indexAnswer, answer);
                      try {
                         } catch (Exception e) {

I got an error for ConcurrentModificationException.

full print:

at java.util.ArrayList$
at ir.mehritco.megnatis.cattell.ui.question.QuestionFragment$4.onCheckedChanged(                                                                      at android.widget.RadioGroup.setCheckedId(
at android.widget.RadioGroup.access$600(
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(
at android.widget.CompoundButton.setChecked(
at android.widget.CompoundButton.toggle(
at android.widget.RadioButton.toggle(
at android.widget.CompoundButton.performClick(
at android.view.View.performClickInternal(
at android.view.View.access$3600(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

what happened here? and way I got this error?
I try some ways like store exam value in another Object and than post exam value or getQuestion from modelView as MutableLiveData but result as same!
My goal is :
Store answer(data) into answer(object)
so for this I need goto exam goto question list get question goto answer list get answer and updated

>Solution :

You’re not supposed to modify collections while iterating except via the iterator. You can use ListIterator to do that:

ListIterator<Answer> iterator = exam.getQuestions()

while (iterator.hasNext()) {
    Answer answer =;
    answer.setAnswer(answer.getId() == checkedId);

Or you could use an indexed loop instead of an iterator.

But in this case there’s no point in calling set() at all, because you’re not updating the answer reference, just the state of the object already in the list. So you can simplify this to a simple for loop or use forEach():

        .forEach(a -> a.setAnswer(a.getId() == checkedId));

Leave a Reply Cancel reply