80

I'm getting this error

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

for the line

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

Following is the entire code. This code was originally in java, I converted it to Kotlin using Android Studio, but now I'm getting this error. I tried rebuilding and cleaning the project, but that didn't work.

val action = supportActionBar //get the actionbar
action!!.setDisplayShowCustomEnabled(true) //enable it to display a custom view in the action bar.
action.setCustomView(R.layout.search_bar)//add the custom view
action.setDisplayShowTitleEnabled(false) //hide the title

edtSearch = action.customView.findViewById(R.id.edtSearch) as EditText //the text editor


//this is a listener to do a search when the user clicks on search button
edtSearch?.setOnEditorActionListener(object : TextView.OnEditorActionListener {
    override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean {
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
         Log.e("TAG","search button pressed")  //doSearch()
         return true
        }
     return false
    }
})
mjsxbo
  • 2,116
  • 3
  • 22
  • 34

7 Answers7

66

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.

zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • 1
    Does this code `onEditorAction(v: TextView, actionId: Int, event: KeyEvent)` insert IDE? If so, that's an error of IDE... – CoolMind Jan 21 '19 at 16:00
16

For me, it happened with the spinner adapter item selector. Below is the correct code for this.

rootView.spinnerState.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
     }
            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }

Make sure the adapter & view are allowed to null i.e (?)

Bharat Lalwani
  • 1,277
  • 15
  • 17
6

To solve this issue, make the event parameter nullable, if you use TextView.OnEditorActionListener. Add ? at the end of the declaration:

override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?)
CoolMind
  • 26,736
  • 15
  • 188
  • 224
Snehal
  • 543
  • 7
  • 11
5

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.

So, add ? to all suspecting variables types: val title: String?.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • If `title` could be null then you should change the type of `title` to be String?. The `?` indicates that the parameter is a nullable type meaning it may be null. Then when you access it, you need to account for the chance that it is null. – SamIAmHarris Aug 28 '19 at 22:40
  • @SamIAmHarris, sure. I didn't know, that `title` could be `null` (it was gotten from JSON). So, I changed to `String?`. I wrote this answer in order to show how this exception can be thrown. – CoolMind Aug 29 '19 at 07:05
5

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:

//java class file
 ClassKotlin cls = new ClassKotlin()
 cls.function1("name", null, true) //Call func from inside kotlin class

but your func declaration in ClassKotlin is:

//Kotlin class file
  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

Hamed Jaliliani
  • 2,789
  • 24
  • 31
3

I struggled a bit with this issue, My issue was in the converters class provided to the Room database class. You can have a closer look into that class and update the variables based on their expectancy of nullability and non-nulability.

Ali Nawaz
  • 2,016
  • 20
  • 30
-1

isMinifyEnabled = false or remove this

in my case, this issue caused due to isMinifyEnabled = true line in gradle file. After removing this line it works.

Ashwin Nirmale
  • 443
  • 4
  • 13