Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using kotlin with ContentResolver caused IllegalArgumentException

Tags:

android

kotlin

I'm try to write an app with Kotlin. I put the contentprovider querying sql on the other process. When querying by ContentResolver in the main process, the App crashed.

The code is:

protected fun select(uri: Uri, sql: String, vararg args: Any): Cursor {
    val sArgs = Array(args.size(), { i -> lang.String.valueOf(args.get(i)) })
    return context.getContentResolver().query(uri, null, sql, sArgs, null)
}

public fun selectAllAccounts(): Array<Account> {
    val cursor = select(parseUri(AccountSchema.TABLE_NAME), "select * from " + AccountSchema.TABLE_NAME)
    val accounts = Array(cursor.getCount(), { i ->
        if (!cursor.moveToPosition(i)) {
            throw RuntimeException("Cursor move failed")
        }
        Account(context, cursor)
    })
    return accounts
}

It will throw an IllegalArgumentException:

05-20 10:27:21.120    3337-3337/com.kescoode.yomail E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.kescoode.yomail, PID: 3337
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kescoode.yomail/com.kescoode.yomail.ui.activity.HomeActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter projection
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
        at dalvik.system.NativeStart.main(Native Method)
 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:167)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:420)
        at android.content.ContentResolver.query(ContentResolver.java:461)
        at android.content.ContentResolver.query(ContentResolver.java:404)
        at com.kescoode.yomail.db.Dao.select(internal.kt:33)
        at com.kescoode.yomail.db.AccountDao.selectAllAccounts(AccountDao.kt:21)
        at com.kescoode.yomail.App.isLogged(App.kt:28)
        at com.kescoode.yomail.ui.activity.HomeActivity.onCreate(HomeActivity.kt:17)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169  
like image 790
Kesco Avatar asked Feb 13 '26 01:02

Kesco


1 Answers

By default all the variables and parameters in Kotlin are non-null. You are passing a null parameter to your function. If you want to pass null parameter you need to add ? symbol to your parameter's type, like uri: Uri?. But better way is to check if the parameter you want to pass to the function is null and handle this situation. For more information you can check this article http://kotlinlang.org/docs/reference/null-safety.html

like image 61
Ilya Vorobiev Avatar answered Feb 14 '26 14:02

Ilya Vorobiev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!