Maria
Maria

Reputation: 387

How to fix java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 on onbindviewholder

I have this app that I am working on and I keep getting this error when I swipe through the viewpager java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 This is where the crash point to. I have tried searching but can't find a solution.

This is how my adapter class looks like: I have a feeling it might be my wrong positioning since after debugging it says the position on this line when (expenseData[position][0].expenseDate) { is 1 yet I pass 0?

class TransactionAdapter(
        private val activity: Activity,
        private val expenseData: ArrayList<ArrayList<ExpenseData>>,
        
        private val totalExpenseAmount: Double)
    : RecyclerView.Adapter<GeneralViewHolder>(), TransactionItemListener {

  

    open class GeneralViewHolder(view: View?) : RecyclerView.ViewHolder(view!!)
    internal class CustomViewHolder(val binding: TransactionsListBinding) : GeneralViewHolder(binding.root) {

        fun setTotalExpenseDate(date: String?) {
            binding.totalExpenseDate.text = date
        }

        fun setTotalExpenseAmount(amount: String?) {
            binding.totalExpenseAmount.text = amount
        }
    }


    override fun getItemViewType(position: Int): Int {
        return if (position == 0) -1 else position
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): GeneralViewHolder {
        val holder: GeneralViewHolder
        val view: View
        if (viewType == -1) {
            view = LayoutInflater.from(activity)
                    .inflate(R.layout.month_summary_card, viewGroup, false)
            holder = MonthSummaryCard(view)
        } else {
            val view1 = TransactionsListBinding.inflate(LayoutInflater.from(activity))
            holder = CustomViewHolder(view1)
        }
        return holder
    }

    override fun onBindViewHolder(holder: GeneralViewHolder, position: Int) {
     
                when (expenseData[position][0].expenseDate) {
                    SimpleDateFormat("MMM dd, yyyy",
                            Locale.US).format(Date()) -> holder1.setTotalExpenseDate("Today")
                    else -> holder1.setTotalExpenseDate(expenseData[position][0].expenseDate)
                }

                expenseData[position].forEach { expenseData ->
                    if (expenseData.expenseType == "Expense") totalExpense += expenseData.expenseAmount else totalIncome += expenseData.expenseAmount
                }
                val amount = totalIncome - totalExpense
                if (amount < 0) holder1.setTotalExpenseAmount(expenseData[position][0].currency + " " +
                        java.lang.Double.valueOf(amount * -1).toString()) else holder1.setTotalExpenseAmount(expenseData[position][0].currency + " " + String.format(Locale.US, "%1$,.2f", amount))
            }
        } catch (e: NullPointerException) {
            Timber.d("Currency null")
        }
    }

    override fun getItemCount(): Int {
        return expenseData.size
    }
}

Will appreciate feedback..

Upvotes: 0

Views: 1183

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93668

In onBindViewHolder, you're accessing expenseData[position][0].expenseDate without checking if expenseData[position] has a non-zero size. It doesn't- you don't have any data there. Why your data is wrong is impossible to tell based on the code you've given.

Upvotes: 1

Related Questions