0

I have tried since last 2 hours to solve this exception..I got the line of code throwing exception.. but didn't find solution..Please help me error occurs @ mUsers.add(user); //in else part

private void readChats()
{

    mUsers = new ArrayList<>();
    databaseReference = FirebaseDatabase.getInstance().getReference("Users");
    databaseReference.addValueEventListener(new ValueEventListener()
    {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                try {
                    mUsers.clear();

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        User user = snapshot.getValue(User.class);

                        //displaying 1 user from chat
                        for (String id : usersList) {
                            if (user.getUserId().equals(id)) {
                                if (mUsers.size() != 0) {
                                    for (User user1 : mUsers) {
                                        if (!user.getUserId().equals(user1.getUserId())) {
                                            mUsers.add(user);
                                        }
                                    }
                                } else {
                                        mUsers.add(user);

                                }
                            }
                        }

                    }//end of for

                    userAdapter = new UserAdapter(getContext(), mUsers);
                    recyclerView.setAdapter(userAdapter);

                }
                catch (Exception e)
                {
                    Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });



}//end of readChats
//this is the method
1
  • You should not modify the container (ArrayList) while you are iterating over it in a for-each loop. Either convert it to a index-based for-loop or create a new ArrayList where you store the planned modifications and do these modifications afte the loop. Commented May 25, 2019 at 6:25

1 Answer 1

1

Because you are editing same ArrayList you are retrieving data. So, its causing concurrency of modification at same time of data access.

Try to edit copied data:

ArrayList<User> tempList = mUsers;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
    User user = snapshot.getValue(User.class);

    //displaying 1 user from chat
    for (String id : usersList) {
        if (user.getUserId().equals(id)) {
            if (mUsers.size() != 0) {
                for (User user1 : mUsers) {
                    if (!user.getUserId().equals(user1.getUserId())) {
                        tempList.add(user);
                    }
                }
            } else {
                tempList.add(user);
            }
        }
    }
}//end of for

//Copy temp list to main list.
mUsers.addAll(tempList);

userAdapter = new UserAdapter(getContext(), mUsers);
recyclerView.setAdapter(userAdapter);
Sign up to request clarification or add additional context in comments.

1 Comment

No Sir..Showing same error.. but I solved using Concurrent Hash map.. tnx for ur response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.