Quantcast
Channel: Mobile - Couchbase Forums
Viewing all articles
Browse latest Browse all 1119

java.lang.IllegalArgumentException: handle must not be 0 when iterating through Array

$
0
0

Hello,
I am running CBLite 2.8.5 on Android. I am seeing an exception being thrown on rare occasions when iterating through an Array and attempting to filter and map it is a Dictionary. Specifically my code is array.filterIsInstance<Dictionary>() (from the Kotlin collections extensions in kotlin-stdlib-common). On rare occasions I can see a stack trace like this:

java.lang.IllegalArgumentException: handle must not be 0
	at com.couchbase.lite.internal.utils.Preconditions.assertNotZero(Preconditions.java:1)
	at com.couchbase.lite.internal.fleece.FLDict.<init>(FLDict.java:2)
	at com.couchbase.lite.internal.fleece.FLValue.asFLDict(FLValue.java:1)
	at com.couchbase.lite.internal.fleece.MDict.initInSlot(MDict.java:5)
	at com.couchbase.lite.internal.fleece.MDict.initInSlot(MDict.java:1)
	at com.couchbase.lite.Dictionary.<init>(Dictionary.java:6)
	at com.couchbase.lite.MValueDelegate.mValueToDictionary(MValueDelegate.java:8)
	at com.couchbase.lite.MValueDelegate.toNative(MValueDelegate.java:5)
	at com.couchbase.lite.internal.fleece.MValue.toNative(MValue.java:2)
	at com.couchbase.lite.internal.fleece.MValue.asNative(MValue.java:3)
	at com.couchbase.lite.Array.getValue(Array.java:1)
	at com.couchbase.lite.Array$ArrayIterator.next(Array.java:1)
    ...

The kotlin stdlib code as my Android Studio defines it:

public inline fun <reified R> Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> {
    return filterIsInstanceTo(ArrayList<R>())
}

/**
 * Appends all elements that are instances of specified type parameter R to the given [destination].
 */
public inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceTo(destination: C): C {
    for (element in this) if (element is R) destination.add(element)
    return destination
}

I cannot supply the raw JSON for this but I do not think that is the problem anyway as this problem does not happen consistently even when the underlying data in the database has not changed. I should also mention that I have only seen this happen on Android 6 devices, but I am not sure if it is due to that or some other hardware difference. Am I doing something wrong here? Are the kotlin Collection helpers not compatible with some underlying mechanism in the couchbase Array class?

In case it is also helpful/relevant, I got the Array instance by using a QueryBuilder.select() call with a WHERE clause equaling a single document ID, got the query result by executing query.execute().allResults().firstOrNull(), got the nested properties by calling result.getDictionary("DB_NAME") (because the select query was a SelectResult.all()), and then finally invoking dictionary.getValue("key") as? Array to get the array.

Thank you,
Cole

13 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1119

Trending Articles