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
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() {
Process: io.keiji.kehai.debug, PID: 13565
java.lang.IndexOutOfBoundsException: The list is empty.
at io.realm.RealmList.first(RealmList.java:463)
// setting.getList().size() is 0.
final WiFiApSetting wifiSetting = setting.getList().first();
if (wifiSetting == null) {
return;
}
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)
@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().
Most helpful comment
@keiji Thank you for reporting this.
This is an intended change related to introducing
OrderedRealmCollection.RealmList.first()andRealmResults.first()had different behavior(one returnsnulland other throws exception) in0.88.In
0.89.0,OrderedRealmCollectionhas been introduced and we need to unify the behavior of those two classes in order to implementOrderedRealmCollection.We should clearly state that in
CHANGELOG.md.If you are using
first()against possible emptyRealmList, please check that withisEmpty()before callingfirst().