Search code examples
androidkotlinandroid-contentproviderandroid-contentresolver

Parameter specified as non-null is null: parameter projection Content Provider Kotlin


I am trying to query content provider from Kotlin file. Please see the code below:

var URI = Uri.parse("content://myprovider")
var nameUri = Uri.withAppendedPath(URI, "name")
cursor = contentResolver.query(nameUri, null, null, null, null)

When I run this code, I am getting below error

Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter projection
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
    at android.content.ContentResolver.query(ContentResolver.java:802)

Now, when I checked the query method signature in ContentResolver class, this is what it is

public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
        @Nullable String[] projection, @Nullable String selection,
        @Nullable String[] selectionArgs, @Nullable String sortOrder)

As you can see, everything apart from Uri can be Nullable, so technically it should not throw this error.

Also, I tried giving projection but then it threw an error for selectionArgs.

Please help. Thanks in advance.

Note: Neither contentResolver not nameUri is null


Solution

  • Can you try this

    override fun query(uri: Uri, projection: Array<String>?, selection: String?,
                   selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
    
    //ur code
    
    return cursor
    }
    

    refer to this link