i am facing ArrayIndexOutOfBoundsException in sorting.
my code is here:
Collections.sort(mutualFriends, new Comparator<FriendInfo>() {
public int compare(FriendInfo s1, FriendInfo s2) {
return s1.name.compareToIgnoreCase(s2.name);
}
});
Logs are here:
STACK_TRACE=java.lang.ArrayIndexOutOfBoundsException
at java.util.Collections.sort(Collections.java:1970)
at com.platinumapps.fragments.Mutual_Friends_Fragment$1.onComplete(Mutual_Friends_Fragment.java:138)
at com.platinumapps.facedroid.AsyncRequestListener.onComplete(AsyncRequestListener.java:59)
at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:328)
and my my mutual friendlist is as:
private List<FriendInfo> mutualFriends = new ArrayList<FriendInfo>();
Any idea to fix this issue. Thanks in advance.
You are most likely having a data race in here.
Some other thread tries to modify the list while it is being sorted (or accesses) – and you get a race condition, which causes in your case the
IndexOutOfBoundsException.It is hard to know where exactly the problem is but some generic ways to solve it are:
synchronizeding on theArrayListobject everywhere it isbeing used –
A common bug that might be the issue in your case is spawning threads, and not waiting for them to finish their work before sorting. This can be solved by simply invoking
join()on all threads before assuming they are done – this ensures you don’t continue until all threads are done.