Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference :KOTLIN

Viewed 384

I am getting this from Logcat

2022-01-07 20:27:46.539 14327-14327/com.example.donedoobnew2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.donedoobnew2, PID: 14327 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.donedoobnew2.Fragments.FragmentMinistries.onCreateView(FragmentMinisteries.kt:29)

FragmentMinistries.kt

class FragmentMinistries : Fragment() {
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?,
): View? {

    val customListData = ArrayList<CList>()
    val customList = CListAdapter(this.requireContext(), customListData)

    customListData.add(CList(R.drawable.ic_call, "First"))
    customListData.add(CList(R.drawable.ic_call, "Second"))
    customListData.add(CList(R.drawable.ic_call, "Third"))

    lv_donedoob_ministries.adapter = customList

    return LayoutInflater.from(container?.context)
        .inflate(R.layout.fragment_ministeries, container, false)
}

}

CListAdapter.kt

class CListAdapter(private val getContext: Context, private val customListItem: ArrayList<CList>) :
ArrayAdapter<CList>(getContext, 0, customListItem) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    var listLayout = convertView
    var holder: ViewHolder

    if (listLayout == null) {
        val inflateList = (getContext as Activity).layoutInflater
        listLayout = inflateList.inflate(R.layout.item_cardview_donedoob, parent, false)

        holder = ViewHolder()
        holder.mTextView = listLayout!!.findViewById(R.id.tv_card_donedoob)
        holder.mImageListView = listLayout.findViewById(R.id.iv_card_donedoob)

        listLayout.tag = holder
    } else {
        holder = listLayout.tag as ViewHolder
    }

    val listItem = customListItem[position]
    holder.mImageListView!!.setImageResource(listItem.mCListImage)
    holder.mTextView!!.text = listItem.mCListText


    return listLayout
}

class ViewHolder {
    internal var mImageListView: ImageView? = null
    internal var mTextView: TextView? = null
}

}

CList.kt

class CList(
var mCListImage: Int,
var mCListText: String,
)

fragment_ministries.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
tools:context=".Fragments.FragmentMinistries">

<ListView
    android:id="@+id/lv_donedoob_ministries"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="10dp"
    android:divider="@color/gray"
    android:padding="20dp">

</ListView>

</FrameLayout>

item_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/round_buttons"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_card_donedoob"
        android:layout_width="180dp"
        android:layout_height="150dp"
        android:src="@drawable/icon_my_documents" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_card_donedoob"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ministry og edu"
            android:textSize="24sp" />

    </LinearLayout>

</LinearLayout>
</LinearLayout>
2 Answers

In this code:

lv_donedoob_ministries.adapter = customList

return LayoutInflater.from(container?.context)
    .inflate(R.layout.fragment_ministeries, container, false)

You are trying to set the adapter on the ListView (lv_donedoob_ministries) before you inflate the layout. You need to inflate the layout first before trying to set the adapter on it.

You are trying to access ListView before inflating fragment view.

class FragmentMinistries : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {

        val customListData = ArrayList<CList>()
        val customList = CListAdapter(this.requireContext(), customListData)

        customListData.add(CList(R.drawable.ic_call, "First"))
        customListData.add(CList(R.drawable.ic_call, "Second"))
        customListData.add(CList(R.drawable.ic_call, "Third"))
        val view = LayoutInflater.from(container?.context)
            .inflate(R.layout.fragment_ministeries, container, false)

        view.findViewById<ListView>(R.id.lv_donedoob_ministries).adapter = customList

        return view
    }
}
Related