Alvaro Falcon Punch Alvaro Falcon Punch - 20 days ago 5
Android Question

Exception has been throw: rowIndex > available rows: 2 > 1

I'm actually doing an application using Realm and I'm having some troubles.

I'm loading a list in my activity with some information stored in my Realm DB, it works pretty fine with 2 elements in the list but when I add the third and try to load the activity, this exception is throw

11-07 16

:54:28.829 11829-11829/com.list.buffet.alvaro.buffetlist E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.list.buffet.alvaro.buffetlist/com.list.buffet.alvaro.buffetlist.SavedListActivity}: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 2 > 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLauchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 2 > 1
at io.realm.internal.TableView.nativeGetSourceRowIndex(Native Method)
at io.realm.internal.TableView.getSourceRowIndex(TableView.java:111)
at io.realm.RealmResults.get(RealmResults.java:215)
at com.list.buffet.alvaro.buffetlist.SavedListActivity.saveListInMap(SavedListActivity.java:166)
at com.list.buffet.alvaro.buffetlist.SavedListActivity.addElementsToList(SavedListActivity.java:156)
at com.list.buffet.alvaro.buffetlist.SavedListActivity.onCreate(SavedListActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 
at android.app.ActivityThread.access$600(ActivityThread.java:165) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5391) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.sys


The method throwing this exception is:

private void saveListInMap() {
for (int i = 0; i < listItems.size(); i++) {
SavedList savedList = new SavedList();
RealmResults<FoodList> queryListName = realm.where(FoodList.class)
.equalTo("listName", listItems.get(i))
.findAll();
savedList.setListName(queryListName.get(i).getListName());
savedList.setRestaurantName(queryListName.get(i).getRestaurantName());
addElementsToList(savedList, queryListName);
plateMap.put(i,savedList);
}
}

private void addElementsToList(SavedList savedList, RealmResults<FoodList> queryListName) {
for (int j = 0; j < queryListName.size(); j++) {
savedList.addToList(queryListName.get(j).getFoodPlate());
}
}


My realm object:

public class FoodList extends RealmObject {
private int id;
private String foodPlate;
private String listName;
private String restaurantName;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFoodPlate() {
return foodPlate;
}

public void setFoodPlate(String foodPlate) {
this.foodPlate = foodPlate;
}

public String getListName() {
return listName;
}

public void setListName(String listName) {
this.listName = listName;
}

public String getRestaurantName() {
return restaurantName;
}

public void setRestaurantName(String restaurantName) {
this.restaurantName = restaurantName;
}
}


I don't know what could be, im a bit frustrated.

Answer

Well I finally got my solution, it was very simple:

private void saveListInMap() {
    for (int i = 0; i < listItems.size(); i++) {
        SavedList savedList = new SavedList();
        RealmResults<FoodList> queryListName = realm.where(FoodList.class)
                .equalTo("listName", listItems.get(i))
                .findAll();

        addElementsToList(savedList, queryListName);
        plateMap.put(i,savedList);
    }
}

private void addElementsToList(SavedList savedList, RealmResults<FoodList> queryListName) {
    for (int j = 0; j < queryListName.size(); j++) {
        savedList.setListName(queryListName.get(i).getListName());
        savedList.setRestaurantName(queryListName.get(i).getRestaurantName());
        savedList.addToList(queryListName.get(j).getFoodPlate());
    }
}

Just had to add the setters into the loop... Otherwise I would be trying to access a position that doesn't exist in the array.

Hope I could help someone with this trouble.