| Tanax |
05-07-2009 03:33 PM |
Okay, so here's something interesting(I can't be bothered to create yet another Java help thread xD).
For those who don't know I've created a bankprogram, with customers, and each customers can have several accounts(savingsaccounts and creditaccounts), and each account you can add money to, or deduct money from. Creditaccounts also has a creditlimit.
Anyhow, the feature that is giving me error, is when I'm trying to remove a customer. What should happen is that it should remove all the belonging accounts, then remove the customer. This is not the case.
Quote:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(T reeMap.java:1100)
at java.util.TreeMap$EntryIterator.next(TreeMap.java: 1136)
at java.util.TreeMap$EntryIterator.next(TreeMap.java: 1131)
at Logic.deleteAccounts(Logic.java:707)
at GUI.actionPerformed(GUI.java:333)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton. java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:1220)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMenuItemUI.java:1261)
at java.awt.Component.processMouseEvent(Component.jav a:6041)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
at java.awt.Component.processEvent(Component.java:580 6)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4413)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 43)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440 )
at java.awt.Component.dispatchEvent(Component.java:42 43)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
|
GUI action performed:
Code:
else if(option.equals("Remove customer"))
{
int selection = JOptionPane.showConfirmDialog(null, "Are you sure?\nThis action cannot be undone.\n", "Delete customer " + this.logic.getName(),
JOptionPane.YES_NO_OPTION);
if(selection == 0)
{
String custName = this.logic.getName();
String accounts = this.logic.deleteAccounts();
double interest = this.logic.removeCustomer();
String text = "Upon deletion of customer " + custName + "(ID: " + this.logic.id() + ") and all belonging accounts(" + accounts + "), ";
text = text + "the total amount of interest was " + interest + "kr!";
this.manageCustomers();
JOptionPane.showMessageDialog(null, text);
}
}
Logic deleteAccounts:
Code:
public String deleteAccounts()
{
SavingsAccount currentSAccount;
CreditAccount currentCAccount;
String accounts = "";
double interest = 0.0;
this.totalInterest = 0.0;
Map sAccounts = this.customer.getSavingsAccounts();
Map cAccounts = this.customer.getCreditAccounts();
Map.Entry account;
Iterator allSavingsAccounts = sAccounts.entrySet().iterator();
Iterator allCreditAccounts = cAccounts.entrySet().iterator();
while(allSavingsAccounts.hasNext())
{
account = (Map.Entry) allSavingsAccounts.next();
currentSAccount = (SavingsAccount) account.getValue();
interest = currentSAccount.prepareDelete();
if(accounts != "") accounts += ", ";
accounts += currentSAccount.getAccountNumber() + ": " + interest;
this.totalInterest += interest;
this.customer.removeAccount(currentSAccount.getAccountNumber());
}
while(allCreditAccounts.hasNext())
{
account = (Map.Entry) allCreditAccounts.next();
currentCAccount = (CreditAccount) account.getValue();
interest = currentCAccount.prepareDelete();
if(accounts != "") accounts += ", ";
accounts += currentCAccount.getAccountNumber() + ": " + interest;
this.totalInterest += interest;
this.customer.removeAccount(currentCAccount.getAccountNumber());
}
return accounts;
}
Line 707:
Code:
account = (Map.Entry) allSavingsAccounts.next();
The thing is, it works great if you only have 1 account. It's when you have more than 1 account that it gets the error. It actually removes 1 account when you click remove customer, then the exceptions are thrown, so the while loop runs one time, but the second time it get's weird. Obviously the customer does not get removed due to the script aborting when the errors occur. But it removes 1 account from the customer(I'm guessing the first account it iterates).
Any ideas???
Reply if you need to see some of the functions I used within these codes.
|