Reputation: 15734
Here is my very, very trimmed down version including only the boolean values and the structure around it:
public int payoffDebt(Double totalDebt) {
boolean booIsPaid[] = new boolean[c.getCount()];
int bb = 0;
int bbb = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
booIsPaid[bb++] = false;
}
while (totalDebt > 0) {
for (int i : rowCounter) {
if ((indBal[p] <= 0) && (booIsPaid[bbb] == false)) {
booIsPaid[bbb++] = true;
}
}
}
return monthTotal;
}
This is what I am trying to do, early on I loop through user "debts" and assign a Boolean value to each one called booIsPaid. They are set to false and this works fine. However, down below, I don't think I am iterating through them correctly because in my Android Application I get Window Leaking LogCat's in my Activity where this is called. I comment out where I set it to true
and all runs fine. Can someone see where it is not right?
Edit: Stacktrace:
12-02 21:14:35.121: E/AndroidRuntime(8832): FATAL EXCEPTION: AsyncTask #1
12-02 21:14:35.121: E/AndroidRuntime(8832): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 21:14:35.121: E/AndroidRuntime(8832): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 21:14:35.121: E/AndroidRuntime(8832): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.lang.Thread.run(Thread.java:856)
12-02 21:14:35.121: E/AndroidRuntime(8832): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
12-02 21:14:35.121: E/AndroidRuntime(8832): at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:290)
12-02 21:14:35.121: E/AndroidRuntime(8832): at com.---.---.Planner$PlannerTask.doInBackground(Planner.java:73)
12-02 21:14:35.121: E/AndroidRuntime(8832): at com.---.---.Planner$PlannerTask.doInBackground(Planner.java:1)
12-02 21:14:35.121: E/AndroidRuntime(8832): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 21:14:35.121: E/AndroidRuntime(8832): ... 5 more
12-02 21:14:35.128: W/ActivityManager(291): Force finishing activity com.---.---/.Planner
12-02 21:14:35.175: W/ActivityManager(291): Force finishing activity com.---.---/.ManageDebts
12-02 21:14:35.285: W/ActivityManager(291): Duplicate finish request for ActivityRecord{41e0b390 com.---.---/.Planner}
12-02 21:14:35.496: I/ActivityManager(291): Displayed com.---.---/.Planner: +557ms
12-02 21:14:35.605: E/WindowManager(8832): Activity com.---.---.Planner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4175e930 that was originally added here
12-02 21:14:35.605: E/WindowManager(8832): android.view.WindowLeaked: Activity com.---.---.Planner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4175e930 that was originally added here
12-02 21:14:35.605: E/WindowManager(8832): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.Window$LocalWindowManager.addView(Window.java:547)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.Dialog.show(Dialog.java:277)
12-02 21:14:35.605: E/WindowManager(8832): at com.---.---.Planner$PlannerTask.onPreExecute(Planner.java:56)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.AsyncTask.execute(AsyncTask.java:534)
12-02 21:14:35.605: E/WindowManager(8832): at com.---.---.Planner.onCreate(Planner.java:36)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.Activity.performCreate(Activity.java:5008)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.Looper.loop(Looper.java:137)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-02 21:14:35.605: E/WindowManager(8832): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 21:14:35.605: E/WindowManager(8832): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 21:14:35.605: E/WindowManager(8832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-02 21:14:35.605: E/WindowManager(8832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-02 21:14:35.605: E/WindowManager(8832): at dalvik.system.NativeStart.main(Native Method)
EDIT: I have reworked everything, here is my new method:
while (totalDebt > 0) {
booIsPaid = false;
remainingBalance = 0;
// Increment month
monthTotal = monthTotal + 1;
// update INDIVIDUAL debt Here
for (int i : rowCounter) {
nameList.add(indName[r++]);
// Zero Balance, set Boolean true, add to Remainder
if ((indBal[p] <= 0) && !booIsPaid) {
booIsPaid = true;
remainingbalance = remainingbalance + indPay[q];
indPay[q] = 0;
indBal[p] = 0;
payList.add(Double.valueOf(IndPay));
// First balance after zero; set Boolean false, add to remainder to payment.
} else if ((indBal[p] > 0) && booIsPaid) {
booIsPaid = false;
indPay[q] = indPay[q] + remainingbalance;
indPay[q] = Math.round(indPay[q] * 100.00) / 100.00;
IndPay = myFormat.format(indPay[q]);
payList.add(Double.valueOf(IndPay));
// Other remaining balances
} else if ((indBal[p] > 0) && !booIsPaid) { indPay[q] = 0;
indPay[q] = Math.round(indPay[q] * 100.00) / 100.00;
IndPay = myFormat.format(indPay[q]);
payList.add(Double.valueOf(IndPay));
}
// indPay[q] (should or should not include remainder here!
indBal[p] = indBal[p] - (indPay[q] - interestFee);
totalDebt = totalDebt - (indPay[q++] - interestFee);
IndBal = myFormat.format(indBal[p++]);
indBalList.add(Double.valueOf(IndBal));
feeList.add(Double.valueOf(InterestFee));
}
}
Note: The output is being stored in the four ArrayLists here which will be printed out to the user on a month by month basis.
The whole practical application is too come up with a debt-payoff strategy. I have yet to test this and will report back. Any feedback? let me know!
Upvotes: 1
Views: 1860
Reputation: 6914
This is your problem:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:290)
You are trying to access an index out of the bounds of your array. Check out the ArrayIndexOutOfBoundsException documentation.
Can you post this specific line code:
DebtDataSource.payoffDebt(DebtDataSource.java:290)
You need to check if the variable bbb
is inside the bounds of your array. I think that is the problem, try changing your code to this:
if ((indBal[p] <= 0) && (booIsPaid[bbb] == false) && (bbb <= booIsPaid.length)){
booIsPaid[bbb++] = true;
}
I will also suggest that you re-set the value of bbb
to zero at some point of your loop code.
Hope it helps!
Upvotes: 2
Reputation: 34367
Some issue/observations below:
There is no need of first loop as boolean []
is already initialized with values as false
There is no need for boolean
comparison with false
, as you can simply write if ((indBal[p] <= 0) && !booIsPaid[bbb]) {
There is no check on the array length(that is causing your issue) in your while loop. Add that as below:
while (totalDebt > 0 && bbb <booIsPaid.length) {
for (int i : rowCounter) {
if (indBal[p] <= 0 && !booIsPaid[bbb] && bbb <booIsPaid.length) {
booIsPaid[bbb++] = true;
}
}
}
Upvotes: 2
Reputation: 106389
First, you don't have to set each value in the boolean array to false
. They're set by default that way.
Second, your loop does look a bit suspect. The following would resolve the AIOOBException you're getting.
while (totalDebt > 0) {
for (int i : rowCounter) {
if (indBal[p] <= 0 && !booIsPaid[bbb]) {
booIsPaid[bbb++] = true;
}
}
bbb = 0;
}
return monthTotal;
The reason: bbb
grows without bound while you're iterating in the while
and for
.
Upvotes: 1