0

I have a class called medicine, I got an error when writing adapter = MedicineAdapter(this, this.medicineList!!) in main activity I saw the error in run window. My app is crashing when I want to run and I saw one error which is about adapter's line

Also, I have medicine_items it is my custom design I assigned it to the adapter in my adapter also I checked my ids but ı don t know why am I got an error

MainActivity

class MainActivity : AppCompatActivity() {


private var adapter: MedicineAdapter? = null
private var medicineList : ArrayList<Medicine>? = null
private var recyclerView: RecyclerView? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    recyclerView =
            findViewById<View>(R.id.recycler) as RecyclerView

    adapter = MedicineAdapter(this, this.medicineList!!)
    val layoutManager = LinearLayoutManager(applicationContext)

    recyclerView!!.layoutManager = layoutManager
    recyclerView!!.itemAnimator = DefaultItemAnimator()

    // Add a neat dividing line between items in the list
    recyclerView!!.addItemDecoration(
            DividerItemDecoration(this,
                    LinearLayoutManager.VERTICAL))

    // set the adapter
    recyclerView!!.adapter = adapter
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
    R.id.addBtn -> {

        val intent = Intent(this,AddNewMedicine::class.java)
        startActivity(intent)


        true
    }
    else -> super.onOptionsItemSelected(item)
}


fun addMedicine(m: Medicine){

    medicineList!!.add(m)
    adapter!!.notifyDataSetChanged()

}

}

MyAdapter

class MedicineAdapter(

    private val mainActivity: MainActivity,
    private val medicineList: ArrayList<Medicine>)

: RecyclerView.Adapter<MedicineAdapter.ListItemHolder>(){


    override fun onCreateViewHolder(
            parent: ViewGroup, viewType: Int): ListItemHolder {

        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.medicine_items, parent, false)

        return ListItemHolder(itemView)

    }
    inner class ListItemHolder(view: View) :
            RecyclerView.ViewHolder(view),
            View.OnClickListener {

        internal var name = view.findViewById<TextView>(R.id.name)

        internal var amount = view.findViewById<TextView>(R.id.amount)

        internal var description = view.findViewById<TextView>(R.id.description)


        init {
            view.isClickable = true
            view.setOnClickListener(this)

        }


        override fun onClick(view: View) {

            //val intentToCarPager = Intent(view!!.context, CarPagerActivity::class.java)

            //view.context.startActivity(intentToCarPager)

        }




    }

    override fun onBindViewHolder(holder: ListItemHolder, position: Int) {

        val medicine = medicineList!![position]

        holder.name.text = medicine.name.toString()

        holder.amount.text = medicine.amount.toString()

        holder.description.text = medicine.desription.toString()



    }

    override fun getItemCount(): Int {
        if (medicineList != null) {
            return medicineList.size
        }
        return -1
    }

}

my run window

8
  • Can you provide your error stacktrace? Commented Jan 9, 2021 at 18:35
  • @mhdwajeeh.95 I have added an image did you want this? I m new in kotlin sorry Commented Jan 9, 2021 at 18:48
  • 1
    Please add the crash report as text not image. Commented Jan 9, 2021 at 18:52
  • What's on line 29 in your MainActivity? I believe you're accessing a null object. Commented Jan 9, 2021 at 18:54
  • @MehranB Stackoverflow says I added too much code so I couldn't Commented Jan 9, 2021 at 18:54

1 Answer 1

1

It's because here:

private var medicineList : ArrayList<Medicine>? = null

you have initialized medicineList as null and you haven't given it a proper value along the way and here:

adapter = MedicineAdapter(this, this.medicineList!!)

you have asserted that it is not null and tried to assign it to the adapter.

If you have no elements to add to your array at first, initialize it this way:

private var medicineList : ArrayList<Medicine> = arrayListOf()

and then you can add elements to it:

medicineList.add(myMedicine)
Sign up to request clarification or add additional context in comments.

5 Comments

yes, the problem is that I have an empty medicineList at first. In my main activity, I have a recycler view and a plus button in the right action bar.If the main activity appears, I will click on my button and switch to another event.I will add some data to my array.
I see. so the answer above should work. are you having any other problems?
main activity appears without crashing right now when I change null to arrayListof() thanks! lastly How can I call my function in main where I add the data, its name is addMedicine
where I add the object is also an activity so how can I call my method in main
First of all, when you have other problems, you don't unmark the correct answer. if you have more questions ask them separately. that's not how Stackoverflow works. I understand you are new to this site but that's not cool. Second, your second questions depends on the architecture of your app and needs more clarification to be answered. so ask a new questions for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.