0

First I gave the exerciseList a null value then inside the lifecycleScope I feched data from a preloaded database but why am I still getting null pointer exception on calling the exerciseList variable?

private var exerciseList: ArrayList<Exercise>? = null
lifecycleScope.launch {
    exerciseList = getExerciseListBySetName(exListName!!)
    setUpUiAndExerciseStatusRV()
}
private suspend fun getExerciseListBySetName(exListName: String): ArrayList<Exercise> {
    val allExerciseList = Constants.getAllExerciseList()
    var ans = ArrayList<Exercise>()
    when (exListName) {
        Constants.MISCELLANEOUS_LIST -> {
            val miscellaneousExercisesDao = (application as HelloHealthyApp).miscellaneousExercisesDb.miscellaneousExercisesDao()
            miscellaneousExercisesDao.fetchAllMiscellaneousExercises().collect {
                val list = ArrayList(it)
                for (i in list) {
                    ans.add(allExerciseList[i.toInt()])
                }
            }
        }
        else -> {
            ans = ArrayList()
        }
    }
    for(exercise in ans) {
        exercise.setIsSelected(false)
        exercise.setIsCompleted(false)
    }
    return ans
}
private fun setUpUiAndExerciseStatusRV() {
    var i = 0
    while (i < exerciseList?.size!!) {
        exercisesArrStr = if (i == exerciseList?.size!! - 1) {
            "$exercisesArrStr${exerciseList?.get(i)?.getId().toString()}"
        } else {
            "$exercisesArrStr${exerciseList?.get(i)?.getId().toString()},"
        }
        i++
    }
    exerciseRVAdapter = ExerciseStatusAdapter(exerciseList!!)
    binding?.rVExerciseStatus?.layoutManager =
        LinearLayoutManager(this@ExerciseActivity, LinearLayoutManager.HORIZONTAL, false)
    binding?.rVExerciseStatus?.adapter = exerciseRVAdapter
    exerciseRVAdapter?.notifyDataSetChanged()
    binding?.exerciseImgView?.setImageResource(
        exerciseList?.get(currentExercisePosition)?.getImage()!!
    )
    binding?.tVTitle?.text = exerciseList?.get(currentExercisePosition)?.getName()
    binding?.upcomingExerciseTextView?.visibility = View.GONE
}
Aditya Jha
  • 11
  • 2
  • I think you should need to use "launch(Dispatcher. Main)" – Hanif Shaikh Nov 07 '22 at 11:14
  • For me it looks like `exerciseList` shouldn't be null. Could you post the exception you get? – Sergio Nov 07 '22 at 11:15
  • @HanifShaikh launch(Dispacher.Main) is not working. Giving the same error "null pointer exception" – Aditya Jha Nov 07 '22 at 11:45
  • @Sergio bro i made exerciseList a non null empty arraylist but now i am getting index out of bound error on calling the values of the arraylist after feching the data. I think the coroutine scope is not working – Aditya Jha Nov 07 '22 at 11:47
  • If you don't like Null Pointer Exceptions crashing your code, don't use `!!` anywhere in your code. You need to learn what nullability is first. `!!` should almost never be used, and definitely not if you don't understand nullability thoroughly. `!!` means "crash my app if this is null". – Tenfour04 Nov 07 '22 at 14:27

0 Answers0