Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
Asked Answered
S

4

34

I got this error just after converted the adapter code to Kotlin:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
    at ...MyAdapter.getView(Unknown Source:35)
    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
    at android.widget.AbsListView.obtainView(AbsListView.java:2366)

The error fires when inflating the row:

class LegalAdapter internal constructor(private val activity: Activity, private val list: ArrayList<Item>) : BaseAdapter() {

    override fun getView(position: Int, convertView: View, parent: ViewGroup): View {

    val layoutInflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    @SuppressLint("ViewHolder")
    val row = layoutInflater.inflate(R.layout.legal_list_item, parent, false) //exception is throw here

Apparently, some parameter that shouldn't be null is null, and kotlin check it. Problem is i can't even debug the new kotlin code.

Stereoscopy answered 15/12, 2018 at 15:49 Comment(0)
M
55

The getView() method is a part of the Adapter interface, and is defined in Java. Documentation here. The important part is this note about the convertView parameter:

View: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using.

This means that it's quite valid for the framework to pass null values for convertView to this method (meaning that you need to create a new view and return that, rather than recycling an old view).

In turn, this means that the Kotlin definition of convertView must be of type View?, not just View. So change your function signature to this:

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View
Mablemabry answered 15/12, 2018 at 16:1 Comment(3)
The signature auto generated by the tool. But yes, it was precisely that. Also, it solved the problem with the debugger. Now it stops again on a breakpoint inside the method.Stereoscopy
Wow! I would have cried for hours trying to figure this thing out. Thanks!Tugman
Worked like a magic, Thank you. This happens we we convert a Java file into Kotlin. If you are using Kotlin when you are creating this BaseAdapter, then you won't have to do check this import signature.Fellatio
P
19

Make convertView nullable:

convertView: View?

I'm not sure why the line number is wrong, but the stacktrace tells you where to look in the error message.

Phatic answered 15/12, 2018 at 15:50 Comment(0)
G
8

Change your convertView in getView from non-null to nullable

override fun getView(position: Int, convertView: View, parent: ViewGroup): View {

to

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
Gerek answered 7/9, 2019 at 13:24 Comment(0)
C
0

All correct solutions are already posted above. Just one more with some code and ViewBinding:

class SpinnerAdapter(
      context: Context,
      private val items: List<YourModel>
) : BaseAdapter() {

private var layoutInflater: LayoutInflater = LayoutInflater.from(context)

override fun getView(i: Int, convertView: View?, viewGroup: ViewGroup): View {
    return if (convertView != null) {
        convertView
    } else {
        val view = layoutInflater.inflate(R.layout.item_spinner, null)
        val binding = ItemSpinnerBinding.bind(view)
        val item = items[i]
        binding.name.text = item.name
        view
    }
}

//more overridde methods
}

Where my R.layout.item_spinner has a TextView with an Id called "name".

Chau answered 29/3, 2020 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.