1
Fatal Exception: java.lang.ArrayIndexOutOfBoundsException
Out of range in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 108(requested: 0 valid: 0) io.realm.internal.OsResults.nativeGetRow

MainActivity.randomizeEvents (MainActivity.java:1600)
...MainActivity$42.run (MainActivity.java:1739)

randomizeEvents()

public void randomizeEvents() {
    Realm nrealm;
    nrealm = Realm.getDefaultInstance();
    RealmResults<Event> eventList = nrealm.where(Event.class).equalTo("theevent", "theevent").findAll();
    if(eventList.size() != 0) {
        evt = eventList.get(0); <<<<<<<<<line 1600
        nrealm.beginTransaction();
        evt.setDurations();
        nrealm.commitTransaction();
    }

}

Method starting Runnable

RealmResults<Event> eventList = realm.where(Event.class).equalTo("theevent", "theevent").findAll();
    evt = eventList.get(0);

    if(!eventTimerRunning) {
        runnable = new Runnable() {
            public void run() {
                if(!realm.isClosed()) {
                    eventTimerRunning = true;
                    randomizeEvents(); <<<<<<<<line 1739
                    handler.postDelayed(runnable, 30000);
                }
            }
        };
        handler.postDelayed(runnable, 5000);
    }

I'm getting this error quite in different parts, but they are all doing the same thing. I have a Runnable for different things. For example this one will run that method every 30s after an initial delay. I am using a new instance of Realm in the Runnable due to errors from using one instance on UI thread and one on runnable etc.

The if statement is if(eventList.size() != 0) so the size is 1+. Why would eventList.size() equal 1 or more, but still get the error about eventList.get(0) being null?

zngb
  • 601
  • 1
  • 7
  • 24

1 Answers1

0

you should use

eventList.size() 

instead of

eventList.size
nishant
  • 2,526
  • 1
  • 13
  • 19
  • That is a typo, real code uses the `size()` method. thanks though, editing to reflect this – zngb Jun 18 '18 at 23:42