aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Array Index Out of Bound Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Array Index Out of Bound Exception" Watch "Array Index Out of Bound Exception" New topic
Author

Array Index Out of Bound Exception

ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
String[] columnTitles = { "Flight #" , "Origin Airport" , "Destination Airport" , "Carrier #" , "Price" , "Day" , "Time" , "Duration" , "Available Seats" , "Select" };
I am trying to show the initial flight information with the check boxes in the last column for the user to select a particular flight for booking the seat.
I am however getting this ArrayOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
at suncertify.client.FlightInformationFrame$EditableTableModel.setValueAt(FlightInformationFrame.java:177)
at javax.swing.JTable.setValueAt(JTable.java:1737)
at suncertify.client.FlightInformationFrame.<init>(FlightInformationFrame.java:93)
at suncertify.client.FlightInformationFrame.main(FlightInformationFrame.java:114)
A portion of my code follows :
String[] columnTitles = { "Flight #" , "Origin Airport" , "Destination Airport" , "Carrier #" , "Price" , "Day" , "Time" , "Duration" , "Available Seats" , "Select" };
for ( int i = 0; i < info.length; i++ ) {
if ( info[i] != null ) {
dataEntries[i] = info[i].getValues();
for ( int j = 0; j < len; j++ ) {
if ( j < ( len - 1 ) )
table.setValueAt( dataEntries[i][j] , i, j );
else
table.setValueAt( new Boolean(false) , i, j );
} // end of for
} // end of if
} // end of for clause

Where am I going wrong ?
-- Ravindra


SCJP, SCJD, SCWCD, SCBCD, SCEA
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

if ( j < ( len - 1 ) )
table.setValueAt( dataEntries[i][j] , i, j );
else
table.setValueAt( new Boolean(false) , i, j );

which one of these setValueAt is line 177?
try putting a System.out.println("Value of i is: " + i + ". Value of j is: " + j"); before each setValueAt.
When you run it, see how far it gets, and add one to j, or maybe i? and you know which value is out of bounds.
Mark
[ March 07, 2002: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Hi Mark
I have added the following SOP in my code. Please have a look at the output.
if ( j < ( len - 1 ) ) {
System.out.println("Setting String Value of i is: " + i + ". Value of j is: " + j);
table.setValueAt( dataEntries[i][j] , i, j );
} else {
System.out.println("Setting Boolean Value of i is: " + i + ". Value of j is: " + j);
table.setValueAt( new Boolean(false) , i, j );
}
j = 8
Setting String Value of i is: 0. Value of j is: 8
dataEntries[i][j] = 50
j = 9
Setting Boolean Value of i is: 0. Value of j is: 9
Thanks
-- Ravindra
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Sorry forgot to paste the rest of the output.
j = 8
Setting String Value of i is: 0. Value of j is: 8
dataEntries[i][j] = 50
j = 9
Setting Boolean Value of i is: 0. Value of j is: 9
java.lang.ArrayIndexOutOfBoundsException
at suncertify.client.FlightInformationFrame$EditableTableModel.setValueAt(FlightInformationFrame.java:182)
at javax.swing.JTable.setValueAt(JTable.java:1737)
at suncertify.client.FlightInformationFrame.<init>(FlightInformationFrame.java:97)
at suncertify.client.FlightInformationFrame.main(FlightInformationFrame.java:119)
Thanks
-- Ravindra
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Therefore the JTable doesn't go to 9.
Well, it's partly true, I just really can't see where the problem is, except that there isn't a 9th column. But your column headers, do add up to 10 columns. Weird.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Index Out of Bound Exception