2

I have List stored in SharedInstance, Which I want to sort based upon time parameter.

Code for sorting Collections.sort(SharedInstance.getInstance().getMasterlistFavFeeds().getPosts(), new DateComparator());

public class DateComparator implements Comparator { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

@Override
public int compare(Object date1, Object date2) {
    try {
        return dateFormat.parse(date2.toString()).compareTo(dateFormat.parse(date1.toString()));
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}

}

Crash Log is

java.lang.ArrayIndexOutOfBoundsException: length=1459; index=1459
    at java.util.Collections.sort(Collections.java:1888)
    at com.fnshealth_android.utils.UpdateComments.updateCommentCountToSharedInstanceList(UpdateComments.java:34)
    at com.fnshealth_android.utils.NotificationManager.processNewCommentFeed(NotificationManager.java:135)
    at com.fnshealth_android.utils.NotificationManager.setDataMessagesMap(NotificationManager.java:99)
    at com.fnshealth_android.services.FNSFirebaseMessagingService.onMessageReceived(FNSFirebaseMessagingService.java:46)
    at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source)
    at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source)
    at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source)
    at com.google.firebase.iid.zzb$2.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

Why I am getting such crash ? and how to handle it ?

Can I use try catch for ArryaoutOfIndex exception ?

KishanCS
  • 1,357
  • 1
  • 19
  • 38
TejasW
  • 43
  • 6
  • 3
    add index - 1 wherever you call arraylist with index. Because index started with 0 and your length is 1459 but index of that length is 1458. Hope it helps – Zaki Pathan Apr 07 '17 at 06:50
  • Hi Zaki , But here I am calling inbuilt method (Collection.sort) and just sending list to it – TejasW Apr 07 '17 at 07:00
  • Why do you want to sort Date objects by converting them into String? I think it is better to sort them as Date. Please check this http://stackoverflow.com/questions/15085608/how-to-sort-an-arraylist-of-date-in-ascending-and-descending-order-which-is-in-s – Blehi Apr 07 '17 at 07:26
  • problem is not here. problem is somewhere else in for loop i think so. Please check if you have any for loop @TejasW – Zaki Pathan Apr 07 '17 at 08:03

0 Answers0