0

When setting my RecyclerView's layoutManager and adpater, I am getting this error:

   java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference

Here's a snippet of my custom RecyclerView.Adapter. I want to put a nested RecyclerView where it is not scrollable, and the size is according to the elements inside it. The context is set when creating the ViewHolder, which I need to pass it to my inner RecyclerView.

//...

private lateinit var context: Context

override fun onCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
): ViewHolder {
    context = parent.context
    val view = LayoutInflater.from(context)
        .inflate(R.layout.date_card, parent, false)
    return ViewHolder(view)
}

override fun getItemCount() = data.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val dateItem = data[position]
    with(holder.itemView) {
        tv_day.text = dateItem.getDayOfMonth()
        tv_day2.text = dateItem.getDayName(resources)
        rv_overview.apply {
            layoutManager = LinearLayoutManager(this@DateAdapter.context)
            adapter = PlanAdapter(dateItem.planItems)
            setHasFixedSize(false)
        }
    }
}

Is there something that I'm missing?

Fawwaz Yusran
  • 1,260
  • 2
  • 19
  • 36

1 Answers1

0

Nevermind, I fixed it! Turns out the nested recyclerview did not have an id. Once I set the id and referenced that, the code is working.

Fawwaz Yusran
  • 1,260
  • 2
  • 19
  • 36