java.lang.IllegalArgumentException : Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull

做~自己de王妃 提交于 2019-12-03 09:30:36

The last parameter can be null, as described by the docs:

KeyEvent: If triggered by an enter key, this is the event; otherwise, this is null.

So what you have to do is make the Kotlin type nullable to account for this, otherwise the injected null check will crash your application when it gets a call with a null value as you've already seen it:

edtSearch?.setOnEditorActionListener(object : TextView.OnEditorActionListener {
    override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
        ...
    }
})

More explanation about platform types in this answer.

To solve this issue, need to make the "event" parameter nullable. Add "?" at the end of the declaration.

override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?)

I got a similar exception: "java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter title".

Then researched a function and found a parameter that became null while must not:

class Item(
    val id: Int,
    val title: String,
    val address: String
)

When I called it like Item(id, name, address) and name was null, I got this exception.

This error would happen if you pass parameters with null value from Java class to Kotlin class [by calling methods & implemented callback between classes].

And even pass null to a parameter that Compiler can not detect it as compile time, so crash will happen in run time.

Because Kotlin is null-safe so the app will crash!

Fix: Change parameters type in kotlin method to Nullable types by adding ? to the end of type.

For example your kotlin funcion will be call in Java class by:

var cls = ClassKotlin()
     cls.function1("name", null, true) //Call func inside kotlin class

but your func declaration in ClassKotlin is:

ClassKotlin {
  fun function1(firstname : String , lastName : String , status : Bool){
  //...
  }
 } // class

So you passed a null value to a non Null paremeter in kotlin func.

How to fix:

Just change the Kotlin func as:

 fun function1(firstname : String , lastName : String? , status : Boolean){
  //...
  }

* a ? added to String data type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!