-3

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.

Pratik
  • 30,639
  • 18
  • 84
  • 159
Mohammad Imran
  • 3,264
  • 4
  • 23
  • 37
  • 1
    The list `mutualFriends` is empty? – Shashank Kadne Jan 16 '13 at 07:23
  • There is insufficient code in your question, because exception throws somewhere else. – Andremoniy Jan 16 '13 at 07:23
  • Whats happening in this `Mutual_Friends_Fragment.java:138` – Faizan Jan 16 '13 at 07:25
  • 3
    My gut tells me there is some *data race* in your code. Is some thread modifying `mutualFriends` concurrently while it is being sorted? (Or in other words: Do you have any multi-threaded jobs in the code that deals with this list)? – amit Jan 16 '13 at 07:29
  • 1
    I agree with @amit, although I would call it _concurrent modification_ instead of _data race_. The only way `Collections.sort` is going to throw an exception like that is if some other code pulls the rug out from underneath it. If you have multiple threads accessing `mutualFriends`, you need to synchronize access. – Ted Hopp Jan 16 '13 at 07:33
  • yes, code is multi-threaded. – Mohammad Imran Jan 16 '13 at 07:34
  • put the code in synchronized block and acquire lock on `mutualFriends` – Faizan Jan 16 '13 at 07:35
  • Basically i am requesting facebook to get mutual fiends and onJSonComplete, i am adding friends to list. At last i call sort method. – Mohammad Imran Jan 16 '13 at 07:37
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Danubian Sailor Sep 27 '21 at 20:01

1 Answers1

5

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:

  1. Make sure your code is thread safe. It can be easily done by synchronizeding on the ArrayList object everywhere it is being used -
  2. Use a class that is designed for thread-safety.

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.

amit
  • 175,853
  • 27
  • 231
  • 333
  • you mean if i write private synchronize List mutualFriends = new ArrayList(); it will solve the issue???? – Mohammad Imran Jan 16 '13 at 08:20
  • @MohammadImran: No, it won't even compile. I suspect writing `synchronized(mutualFriends)` before every point that the list is being used will solve the issue (though with very bad performance), but as I also said - cannot be sure without more code. – amit Jan 16 '13 at 08:23
  • Basically after parsing JSon,i am sorting list and then Update UI. I think it will slow down the performance, ANR can occur. would it be better to use asynTask instead of sunchronized block???? – Mohammad Imran Jan 16 '13 at 09:52