Android Studio, app crashes when changing MP BarChart data

Solution for Android Studio, app crashes when changing MP BarChart data
is Given Below:

I found multiple forms with similar issue, but none of those led to s solution.

I am trying to create a barChart in android using MPlibrary to show money spend each day. I have two buttons
‘WEEKLY BUTTON’ : shows expenses for past seven days(7 chart bars, 7 X-axis labels)
‘MONTHLY BUTTON’ : shows expenses for past month in quarters(4 chart bars, 4 X-axis labels)

there is only ONE barChart and the above buttons updates that barchart

both buttons work fine, except the app crashes when I switch from ‘WEEKLY’ to ‘MONTHLY’ button.
Here is my code:

barChart = root.findViewById(R.id.barChart);
        BarDataSet barDataSet = new BarDataSet(expenseModelData.getExpenseBarEntries(), "Expenses($)");
        barDataSet.setValueTextColor(getResources().getColor(R.color.black));
        barDataSet.setColor(getResources().getColor(R.color.design_default_color_primary_dark));
        barDataSet.setValueTextSize(16f);
        BarData barData = new BarData(barDataSet);
        barChart.setData(barData);
        barChart.setTouchEnabled(true);
        barChart.setDragEnabled(true);
        barChart.setScaleEnabled(true);
        barChart.getDescription().setEnabled(false);

        ValueFormatter formatter = new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return expenseModelData.getExpenseDates()[(int) value];
            }
        };

        XAxis xAxis = barChart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setValueFormatter(formatter);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);



         weekly_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expenseModelData.setDataForWeeklyExpense();
                expenseModelData.generateBarChart(barChart, expenseModelData.getExpenseBarEntries(), expenseModelData.getExpenseDates(), getActivity());
            }
        });

        monthly_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expenseModelData.setDataForMonthlyExpense();
                expenseModelData.generateBarChart(barChart, expenseModelData.getExpenseBarEntries(), expenseModelData.getExpenseDates(), getActivity());
            }
        });

I created generateBarChart() method to update the barChart

public void generateBarChart(BarChart barChart, ArrayList<BarEntry> currExpenseBarEntries, String[] currExpenseDates, Context context) {
        //barChart.invalidate();
        barChart.clear();
        BarDataSet barDataSet = new BarDataSet(currExpenseBarEntries, "Expenses($)");
        barDataSet.setValueTextColor(context.getResources().getColor(R.color.black));
        barDataSet.setColor(context.getResources().getColor(R.color.design_default_color_primary_dark));
        barDataSet.setValueTextSize(16f);
        BarData barData = new BarData(barDataSet);
        barChart.setData(barData);
        barChart.setTouchEnabled(true);
        barChart.setDragEnabled(true);
        barChart.setScaleEnabled(true);
        barChart.getDescription().setEnabled(false);
        barChart.notifyDataSetChanged();

        ValueFormatter formatter = new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return currExpenseDates[(int) value];
            }
        };

        XAxis xAxis = barChart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setValueFormatter(formatter);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);


    }

My stack trace and some research shows that problem is with my ValueFormatter. I’m getting IndexOutOfBound Exception. Things I have tried so far:

  • removed all ValueFormatter lines (app works fine but I loose my correct x-axis labels)
  • add/remove ValueFormatter lines from main and method code (tried all add/remove combinations)
  • added barChart.notifyDataSetChanged();
  • added barChart.invalidate() && barChart.clear()
  • made sure that my barData and x-axis labels are updating properly (they are)

nothing fixed the problem. I will appreciate any help.