I have a table full of calculations. Each row contains numbers based on personal debt. For example:
Row 1: Debt 1 (Name), 7.9 (APR), 1000 (Balance), 20 (MinPayment);
Row 2: Debt 2 (Name), 9.9 (APR), 2000 (Balance), 40 (MinPayment);
etc..
There are 6 rows in this test. I am doing a few loops with a few arrays and I got the above error (in title).
05-17 18:31:47.548: E/AndroidRuntime(2019): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
Which is odd because length IS 6?
Here is the entire Method:
public int payoffDebt(Double totalDebt) {
Cursor c = database.rawQuery("SELECT * FROM debt;", null);
int monthTotal = 0;
double interestFee = 0;
double interestFeeTotal = 0;
String indBal[] = new String[c.getCount()];
String indPay[] = new String[c.getCount()];
String indApr[] = new String[c.getCount()];
double totalBal[] = new double[c.getCount()];
double totalInterestFees[] = new double[c.getCount()];
int rowCounter[] = new int[c.getCount()];
int j = 0; int k = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
// get individual Bal, APR percent, Payment, store into three arrays;
indBal[k++] = c.getString(c.getColumnIndex("debt_total"));
indPay[k++] = c.getString(c.getColumnIndex("payment"));
indApr[k++] = c.getString(c.getColumnIndex("apr"));
rowCounter[k++] = k;
}
c.close();
while (totalDebt >= 0) {
for (int i : rowCounter) {
interestFee = (((Double.valueOf(indApr[i]) / 100) / 12) * Double
.valueOf(indBal[i]));
totalDebt = totalDebt
- (Double.valueOf(indPay[i]) - interestFee);
interestFeeTotal += interestFee; // sum of all Apr Fees CURRENT
// month in while loop
}
totalBal[j++] = totalDebt; // record total debt for this after all
// payments
totalInterestFees[j++] = interestFeeTotal; // record total interest
// fees for this month
// from all debt
// Increment month
monthTotal += 1;
}
return monthTotal;
The problem was line 165 :
indApr[k++] = c.getString(c.getColumnIndex("apr"));
kall that many times?kfour times for each loop iteration