Realm-java: Updating realm to 0.89.0 RealmList#first() throw IndexOutOfBoundsException at RealmList size is 0.

Created on 26 Apr 2016  路  4Comments  路  Source: realm/realm-java

Empty RealmList#first() throw IndexOutOfBoundsException instead of return null.

This issue is very similar to below issue.

https://github.com/realm/realm-java/issues/2659

Expected Results

RealmList#first() result is null;

/**
 * Finds the first object.
 *
 * @return the first object or {@code null} if the list is empty.
 * @throws IllegalStateException if Realm instance has been closed or parent object has been removed.
 */
public E first() {

Actual Results

Process: io.keiji.kehai.debug, PID: 13565
java.lang.IndexOutOfBoundsException: The list is empty.
 at io.realm.RealmList.first(RealmList.java:463)

Code Sample

// setting.getList().size() is 0.
final WiFiApSetting wifiSetting = setting.getList().first();

if (wifiSetting == null) {
    return;
}

Version of Realm and tooling

Realm version(s): 0.89.1

Android Studio version: 2.0 (Stable)
gradle 2.0.0
buildToolsVersion 23.0.2

Which Android version and device: Nexus 5X (Android 6.0.1)

T-Doc

Most helpful comment

@keiji Thank you for reporting this.

This is an intended change related to introducing OrderedRealmCollection.

RealmList.first() and RealmResults.first() had different behavior(one returns null and other throws exception) in 0.88.
In 0.89.0, OrderedRealmCollection has been introduced and we need to unify the behavior of those two classes in order to implement OrderedRealmCollection.

We should clearly state that in CHANGELOG.md.

If you are using first() against possible empty RealmList, please check that with isEmpty() before calling first().

All 4 comments

@keiji Thank you for reporting this.

This is an intended change related to introducing OrderedRealmCollection.

RealmList.first() and RealmResults.first() had different behavior(one returns null and other throws exception) in 0.88.
In 0.89.0, OrderedRealmCollection has been introduced and we need to unify the behavior of those two classes in order to implement OrderedRealmCollection.

We should clearly state that in CHANGELOG.md.

If you are using first() against possible empty RealmList, please check that with isEmpty() before calling first().

Well the docs do say

 * @return the first object or {@code null} if the list is empty.

@Zhuinden Ups, look like mistake. That Javadoc should have been removed. You can see the correct behavior described in OrderedRealmCollection

I got tripped up on this too recently. Javadocs for RealmList are still wrong for first() and last().

Was this page helpful?
0 / 5 - 0 ratings