manuman1968
asked on
URGENT Java Array out of bounds exception
Hi experts.
I have a problem, i am working on an assignment for a banking system and have run into an exception problem that i am struggling to overcome and i am allready past my deadline, so this is very urgent. The following is the exception:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB oundsExcep tion: -1
this exception is showing as being in my Branch class as below
public Customer getCustomerAt( int index )
{
if( index >= theCustList.length)
return null;
else
return theCustList[index];
}
and in my GUI class as below
currentSelectedCustomer = branch.getCustomerAt(custo merComboBo x.getSelec tedIndex() );
The app works fine when i am viewing and performing operations on customers that i built as default into the system for testing, however as soon as i try to add a new customer to the system the combobox that displays the customers for selection goes blank and the exception is thrown. I have been trying to sort this for days now and i am lost. I am comiling the program in Netbeans 5.5 IDE but have also tried it in Eclipse in case it was a Netbeans bug ( I know im desperate) but with the same results. Can anybody help me urgentley. 500 points. Thanks in advance
I have a problem, i am working on an assignment for a banking system and have run into an exception problem that i am struggling to overcome and i am allready past my deadline, so this is very urgent. The following is the exception:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
this exception is showing as being in my Branch class as below
public Customer getCustomerAt( int index )
{
if( index >= theCustList.length)
return null;
else
return theCustList[index];
}
and in my GUI class as below
currentSelectedCustomer = branch.getCustomerAt(custo
The app works fine when i am viewing and performing operations on customers that i built as default into the system for testing, however as soon as i try to add a new customer to the system the combobox that displays the customers for selection goes blank and the exception is thrown. I have been trying to sort this for days now and i am lost. I am comiling the program in Netbeans 5.5 IDE but have also tried it in Eclipse in case it was a Netbeans bug ( I know im desperate) but with the same results. Can anybody help me urgentley. 500 points. Thanks in advance
Is the logic right?
When the index is greater than theCustList.length, you return null but when it is less than theCustList.length which could be negative values, you return theCustList[index].
I just do not feel that it is correct logic.
David
When the index is greater than theCustList.length, you return null but when it is less than theCustList.length which could be negative values, you return theCustList[index].
I just do not feel that it is correct logic.
David
ASKER
Yes the error message shows that when i try to add a customer the array has an index of -1.
When i try to put a print ln in i can for viewing the customers but when i try to add i just get the error message again and no print out. I have tried putting a printline in both at the customer index and also in the gui for the combo box. Below is what i get
Blackburn Branch now has 4 customer(s)
Elihe.Customer@145d068
Elihe.Customer@3e86d0
Elihe.Customer@a39137
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB oundsExcep tion: -1
at Elihe.Branch.getCustomerAt (Branch.ja va:70)
at Elihe.BankFrame.actionPerf ormed(Bank Frame.java :204)
at javax.swing.JComboBox.fire ActionEven t(JComboBo x.java:119 7)
at javax.swing.JComboBox.cont entsChange d(JComboBo x.java:126 8)
at javax.swing.JComboBox.inte rvalRemove d(JComboBo x.java:128 8)
at javax.swing.AbstractListMo del.fireIn tervalRemo ved(Abstra ctListMode l.java:161 )
at javax.swing.DefaultComboBo xModel.rem oveAllElem ents(Defau ltComboBox Model.java :169)
at javax.swing.JComboBox.remo veAllItems (JComboBox .java:745)
at Elihe.BankFrame.populateCu stomersNam es(BankFra me.java:12 5)
at Elihe.BankFrame.actionPerf ormed(Bank Frame.java :219)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :1849)
at javax.swing.AbstractButton $Handler.a ctionPerfo rmed(Abstr actButton. java:2169)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del.java:4 20)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 258)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (BasicButt onListener .java:236)
at java.awt.Component.process MouseEvent (Component .java:5488 )
at javax.swing.JComponent.pro cessMouseE vent(JComp onent.java :3126)
at java.awt.Component.process Event(Comp onent.java :5253)
at java.awt.Container.process Event(Cont ainer.java :1966)
at java.awt.Component.dispatc hEventImpl (Component .java:3955 )
at java.awt.Container.dispatc hEventImpl (Container .java:2024 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3803)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:4 212)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:38 92)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3822)
at java.awt.Container.dispatc hEventImpl (Container .java:2010 )
at java.awt.Window.dispatchEv entImpl(Wi ndow.java: 1778)
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3803)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:463)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hThread.ja va:242)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hread.java :163)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 57)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 49)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:110)
I appear to be gaining a customer from somewhere as a println i added earlier to try to find the fault shows the system as having 4 customers when in fact there are only the default 3.
Any ideas, i know i am getting a wrong index when i try to add a new customer but i do not know how to correct it.
Many Thanks
When i try to put a print ln in i can for viewing the customers but when i try to add i just get the error message again and no print out. I have tried putting a printline in both at the customer index and also in the gui for the combo box. Below is what i get
Blackburn Branch now has 4 customer(s)
Elihe.Customer@145d068
Elihe.Customer@3e86d0
Elihe.Customer@a39137
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
at Elihe.Branch.getCustomerAt
at Elihe.BankFrame.actionPerf
at javax.swing.JComboBox.fire
at javax.swing.JComboBox.cont
at javax.swing.JComboBox.inte
at javax.swing.AbstractListMo
at javax.swing.DefaultComboBo
at javax.swing.JComboBox.remo
at Elihe.BankFrame.populateCu
at Elihe.BankFrame.actionPerf
at javax.swing.AbstractButton
at javax.swing.AbstractButton
at javax.swing.DefaultButtonM
at javax.swing.DefaultButtonM
at javax.swing.plaf.basic.Bas
at java.awt.Component.process
at javax.swing.JComponent.pro
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
I appear to be gaining a customer from somewhere as a println i added earlier to try to find the fault shows the system as having 4 customers when in fact there are only the default 3.
Any ideas, i know i am getting a wrong index when i try to add a new customer but i do not know how to correct it.
Many Thanks
Can you post your codes to add the new customer and the latest codes that you used to print out the customer?
David
David
ASKER
Hi David,
That sounds exactly like what my problem is. I am new to programming and do not know how to get round the problem. What should the correct logic be there.
Thanks
That sounds exactly like what my problem is. I am new to programming and do not know how to get round the problem. What should the correct logic be there.
Thanks
Not a problem,
As long as you show us that you put the efforts to do it, we will be happy to help.
Can you post the complete codes so that we can have a look?
David
As long as you show us that you put the efforts to do it, we will be happy to help.
Can you post the complete codes so that we can have a look?
David
Problem is in this line:
currentSelectedCustomer = branch.getCustomerAt(custo merComboBo x.getSelec tedIndex() );
This returns -1 if nothing is selected.
First check whether the user selected something with:
if (customerComboBox.getSelec tedIndex() > 0)
{
//... ok user selected something
}
else
{
//... nothing selected
}
See:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#getSelectedIndex()
Mark
currentSelectedCustomer = branch.getCustomerAt(custo
This returns -1 if nothing is selected.
First check whether the user selected something with:
if (customerComboBox.getSelec
{
//... ok user selected something
}
else
{
//... nothing selected
}
See:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#getSelectedIndex()
Mark
ASKER
Here are the codes David. I am actually using a gui to display the customers
This is the code for the administration of a branch where i add customers
/*
* Branch.java
*
* Created on 14 March 2007, 10:29
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Elihe;
/**
*
* @author brian britwell
*/
public class Branch{
private String theName;
private Customer[] theCustList;
private int numCustomers;
private int maxCustomers;
/** Creates a new instance of Branch */
public Branch(String aName, int maxNum){ //Constructor method
maxCustomers = maxNum;
theCustList = new Customer[maxCustomers];
theName = aName;
}
public boolean addCustomer(Customer newCustomer) {
//if there is more place for the new customer...
if (numCustomers < maxCustomers) {
theCustList[numCustomers] = newCustomer;
numCustomers++;
System.out.println(theName );
System.out.println("New customer added");
System.out.println( theName + " now has " + (numCustomers+1) + " customer(s)");
return true;
} else {
System.out.println("New customer not added");
System.out.println( theName + " already has a full set of customers");
return false;
}
}
//Selector methods
public String getName() {
return theName;
}
public int getNumCustomers() {
return numCustomers;
}
public Customer[] getCustomers() {
return theCustList;
}
//returns the customer at the specified location
public Customer getCustomerAt( int index )
{
if( index >= theCustList.length)
return null;
else
// System.out.println(theCust List[index ]);
return theCustList[index];
}
public void printCustomersDetails() {
System.out.println("Custom ers for branch: " + theName);
for (int i = 0; i < theCustList.length; i++)
theCustList[i].printCustom erDetails( );
System.out.println("------ ---------- ---------- -");
}
}
This is from the GUI for viewing the default Cutomers
public void actionPerformed(ActionEven t event){
//depending on the control that trigged the event...do some action
if(event.getSource() == customerComboBox ){
//get the customer from the branch, and store it locally
System.out.println(branch. getCustome rAt(custom erComboBox .getSelect edIndex()) );
currentSelectedCustomer = branch.getCustomerAt(custo merComboBo x.getSelec tedIndex() );
//print it's name in the field
customerNameTextField.setT ext(curren tSelectedC ustomer.ge tName());
//prints in the frame the details of the current selected account of the current selected customer
putOnScreenAccountDetails( );
}
and this is when i try to add new customers
else
if(event.getSource() == addCustomerButton ){
populateCustomersNames();
//create customer
Customer c1 = new Customer(customerNameTextF ield.getTe xt());
//create one of the each type of account for customer, with some default values
CreditCardAccount cca1 = new CreditCardAccount("cca"+br anch.getNu mCustomers (),0,100,5 );
SavingsAccount sa1 = new SavingsAccount("sa"+branch .getNumCus tomers(),0 ,5);
CurrentAccount ca1 = new CurrentAccount("ca"+branch .getNumCus tomers(),0 ,300);
//set the accounts to the customer
c1.setCreditCardAccount(cc a1);
c1.setCurrentAccount(ca1);
c1.setSavingsAccount(sa1);
//add the customer to the branch
branch.addCustomer(c1);
//called to put the customers name into the combobox (repopulate)
populateCustomersNames();
}
And this for displaying the details in the GUI
private void putOnScreenAccountDetails( ){
//depending on the account type....
if(accountTypeComboBox.get SelectedIn dex() == 0){
CreditCardAccount acc = currentSelectedCustomer.ge tCreditCar dAccount() ;
accountNumberTextField.set Text(acc.g etAccNumbe r());
balanceTextField.setText(" "+acc.getB alance());
detailsTextField.setText(" Limit: " + acc.getLimit() +", Rate: " + acc.getSavingsRate());
}
else
if(accountTypeComboBox.get SelectedIn dex() == 1){
CurrentAccount acc = currentSelectedCustomer.ge tCurrentAc count();
accountNumberTextField.set Text(acc.g etAccNumbe r());
balanceTextField.setText(" "+acc.getB alance());
detailsTextField.setText(" Limit: " + acc.getLimit());
}
else
if(accountTypeComboBox.get SelectedIn dex() == 2){
SavingsAccount acc = currentSelectedCustomer.ge tSavingsAc count();
accountNumberTextField.set Text(acc.g etAccNumbe r());
balanceTextField.setText(" "+acc.getB alance());
detailsTextField.setText(" Rate: " + acc.getSavingsRate());
}
}
Thanks hope you can help
This is the code for the administration of a branch where i add customers
/*
* Branch.java
*
* Created on 14 March 2007, 10:29
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Elihe;
/**
*
* @author brian britwell
*/
public class Branch{
private String theName;
private Customer[] theCustList;
private int numCustomers;
private int maxCustomers;
/** Creates a new instance of Branch */
public Branch(String aName, int maxNum){ //Constructor method
maxCustomers = maxNum;
theCustList = new Customer[maxCustomers];
theName = aName;
}
public boolean addCustomer(Customer newCustomer) {
//if there is more place for the new customer...
if (numCustomers < maxCustomers) {
theCustList[numCustomers] = newCustomer;
numCustomers++;
System.out.println(theName
System.out.println("New customer added");
System.out.println( theName + " now has " + (numCustomers+1) + " customer(s)");
return true;
} else {
System.out.println("New customer not added");
System.out.println( theName + " already has a full set of customers");
return false;
}
}
//Selector methods
public String getName() {
return theName;
}
public int getNumCustomers() {
return numCustomers;
}
public Customer[] getCustomers() {
return theCustList;
}
//returns the customer at the specified location
public Customer getCustomerAt( int index )
{
if( index >= theCustList.length)
return null;
else
// System.out.println(theCust
return theCustList[index];
}
public void printCustomersDetails() {
System.out.println("Custom
for (int i = 0; i < theCustList.length; i++)
theCustList[i].printCustom
System.out.println("------
}
}
This is from the GUI for viewing the default Cutomers
public void actionPerformed(ActionEven
//depending on the control that trigged the event...do some action
if(event.getSource() == customerComboBox ){
//get the customer from the branch, and store it locally
System.out.println(branch.
currentSelectedCustomer = branch.getCustomerAt(custo
//print it's name in the field
customerNameTextField.setT
//prints in the frame the details of the current selected account of the current selected customer
putOnScreenAccountDetails(
}
and this is when i try to add new customers
else
if(event.getSource() == addCustomerButton ){
populateCustomersNames();
//create customer
Customer c1 = new Customer(customerNameTextF
//create one of the each type of account for customer, with some default values
CreditCardAccount cca1 = new CreditCardAccount("cca"+br
SavingsAccount sa1 = new SavingsAccount("sa"+branch
CurrentAccount ca1 = new CurrentAccount("ca"+branch
//set the accounts to the customer
c1.setCreditCardAccount(cc
c1.setCurrentAccount(ca1);
c1.setSavingsAccount(sa1);
//add the customer to the branch
branch.addCustomer(c1);
//called to put the customers name into the combobox (repopulate)
populateCustomersNames();
}
And this for displaying the details in the GUI
private void putOnScreenAccountDetails(
//depending on the account type....
if(accountTypeComboBox.get
CreditCardAccount acc = currentSelectedCustomer.ge
accountNumberTextField.set
balanceTextField.setText("
detailsTextField.setText("
}
else
if(accountTypeComboBox.get
CurrentAccount acc = currentSelectedCustomer.ge
accountNumberTextField.set
balanceTextField.setText("
detailsTextField.setText("
}
else
if(accountTypeComboBox.get
SavingsAccount acc = currentSelectedCustomer.ge
accountNumberTextField.set
balanceTextField.setText("
detailsTextField.setText("
}
}
Thanks hope you can help
Okay,
Where is the codes of populateCustomersNames();?
I guess that the problem occurred here.
Where is the codes of populateCustomersNames();?
I guess that the problem occurred here.
I will be leaving in 10-15 mins...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Codes for populateCustomersNames
//called to put the customers name into the combobox
private void populateCustomersNames(){
//read all the customers from the branch
Customer[] cust = branch.getCustomers();
//clear previous ones
customerComboBox.removeAll Items();
//add each name
for( int i = 0 ; i < branch.getNumCustomers() ; i++ ){
customerComboBox.addItem(c ust[i].get Name());
}
}
Again Thanks
//called to put the customers name into the combobox
private void populateCustomersNames(){
//read all the customers from the branch
Customer[] cust = branch.getCustomers();
//clear previous ones
customerComboBox.removeAll
//add each name
for( int i = 0 ; i < branch.getNumCustomers() ; i++ ){
customerComboBox.addItem(c
}
}
Again Thanks
Sorry,
I got to go now for a meeting. I will have a look at this later. Hopefully, by then, this has been solved by someone here.
David
I got to go now for a meeting. I will have a look at this later. Hopefully, by then, this has been solved by someone here.
David
and please try ADSLMark's solution too.
ASKER
Thanks Mark
Thats fixed it, i just need to sort the index numbering now for my display.
Who should i award the points to, yourself or split them as you have both helped.
Again many thanks from a newcomer
Thats fixed it, i just need to sort the index numbering now for my display.
Who should i award the points to, yourself or split them as you have both helped.
Again many thanks from a newcomer
It should go to Mark only.
and glad that it worked.
David
and glad that it worked.
David
it seems your index has an irrelevant value such as -1 or -2