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
}