21

I have the following ViewHolder class for my Recycler View,

inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val dateText = itemView.itemDateSummaryList
        private val systolicVal = itemView.systolicValue
        private val diastolicVal = itemView.diastolicValue

        fun update(listItem: SummaryListItemModel) {
            Log.i(TAG, "Update method called " + listItem.date)
            dateText.text = listItem.date
            systolicVal.text = listItem.sysVal.toInt().toString()
            diastolicVal.text = listItem.diasVal.toInt().toString()
        }

    }

But when I run the app an error comes up at the dateText.text = listItem.date saying,

java.lang.IllegalStateException: dateText must not be null
at *****.******.*****.ui.detailview.bloodpressure.SummaryListAdapter$ItemViewHolder.update(SummaryListAdapter.kt:68)

But the listItem.date is not null I have check with the Log.

kokilayaa
  • 579
  • 4
  • 7
  • 19

9 Answers9

14

the error is not about listItem.date, the error says that the dateText textview to which you are trying to set text is null ,

double check you are using the correct textview

Possibilities :
1) you might be using wrong id of textview
2) you may have used wrong file while inflating view.

ctrl + click on itemDateSummaryList and see whether the textview is from the same layout file you have infate or otherwise

Shankar
  • 2,890
  • 3
  • 25
  • 40
  • 1
    Oh my god, all my fragments are converted activities and the first time I converted one, I just copy pasted its onCreateView override and forgot to change the inflated layout as I paste it on other new fragments. This should be the accepted answer. Damn. Simple mistake and it took me 3 hours – Abana Clara Oct 23 '20 at 03:17
8

If you are using fragments: 1. Use a global variable that will hold your layout reference

private var root: View? = null   // create a global variable which will hold your layout 
 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            root = inflater.inflate(R.layout.your_layout_file_name, container, false)  // initialize it here
            return root
    }
  1. To use any view of your layout which is inside your layout you can use it with (root.) like:

root?.textView.text="Hello"

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
  • Point to note, if your view is inside a navigation header or something of that sort then youhave to initialise those view. – Rohan Bojja Aug 07 '19 at 08:24
  • EDIT: Point to note, if your view is inside a navigation header or something of that sort, then you have to initialise those views first. Something like, val navView: NavigationView = findViewById(R.id.nav_view) val navHeader = navView.getHeaderView(0) val textView = navHeader.findViewById(R.id.textView) as TextView – Rohan Bojja Aug 07 '19 at 08:39
4

It's saying your dateText view itself is null not the value listItem.date Verify whether you are rendering it correctly, the itemview has the TextView with the id you are trying to access

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
2

The same happened to me while using Fragment, I took my code from onCreateView and put it to onViewCreated method, the problem solved.

SinaMN75
  • 6,742
  • 5
  • 28
  • 56
2

I get this error my fragment, I solved this error,

Error's Code:

class MyFragment : Fragment(){

    private var item: Item? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        item = arguments?.getParcelable(BUNLDE_ITEM)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        item?.let {
            textViewTitle.text = "Text"
        }
        return inflater.inflate(R.layout.my_fragment, null, false)
    }

    companion object {
        private const val BUNLDE_ITEM = "item"
        fun newInstance(item: Item) = MyFragment().apply {
            arguments = Bundle().apply {
                putParcelable(BUNLDE_ITEM, item)
            }
        }
    }
}

Working Code:

class MyFragment : Fragment() {

    private var item: Item? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        item = arguments?.getParcelable(BUNLDE_ITEM)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val root: View = inflater.inflate(R.layout.my_fragment, container, false)
        val textViewTitle = root.findViewById<View>(R.id.textViewTitle) as TextView

        item?.let {
            textViewTitle.text = "text"
        }
        return root
    }

    companion object {
        private const val BUNLDE_ITEM = "item"
        fun newInstance(item: Item) = MyFragment().apply {
            arguments = Bundle().apply {
                putParcelable(BUNLDE_ITEM, item)
            }
        }
    }
}
Adrian W
  • 4,563
  • 11
  • 38
  • 52
Ogulcan Ucarsu
  • 236
  • 2
  • 5
2

It also might happen on wrong context reference in xml file, usually it appears on manualy moving Activity to another package, Android studio didn't make this change automatically:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:tools="http://schemas.android.com/tools"
        ....
        tools:context=".view.ui.login.LoginActivity">
....
</androidx.constraintlayout.widget.ConstraintLayout>
Vitaliy A
  • 3,651
  • 1
  • 32
  • 34
1

I read all of these answers but none worked for me so here is my answer.

Find the line of code similar the this:

var view:View?=inflater.inflate(R.layout.fragment_profilefragement, container, false)

Place your cursor on fragement_ProfileFragement and click with CTRL. Check that you are in the right fragement XML file.

JBES
  • 1,512
  • 11
  • 18
0

I just had the same issue with my RecyclerView. It had been working for months. After numerous unsuccessful troubleshooting steps, I did a Build > Clean Project and it fixed the issue.

0

You have look carefully because IN SOME PART OF YOUR CODE you are not referencing the view correctly. In other words, that specific view does not exist in your code.

  1. Try to repeat the steps you did
  2. Check in the class where the code fails your view is being inflated correctly.

This will save you hours of looking where is wrong.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hanako
  • 1,637
  • 1
  • 13
  • 16