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.
Make
convertView
nullable:I'm not sure why the line number is wrong, but the stacktrace tells you where to look in the error message.
The
getView()
method is a part of theAdapter
interface, and is defined in Java. Documentation here. The important part is this note about theconvertView
parameter:This means that it's quite valid for the framework to pass
null
values forconvertView
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 typeView?
, not justView
. So change your function signature to this:Change your convertView in getView from non-null to nullable
to
All correct solutions are already posted above. Just one more with some code and ViewBinding:
Where my R.layout.item_spinner has a TextView with an Id called "name".