• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Index Out of Bound Exception

 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is as follows
String[] columnNames = { "Flight #" , "Origin Airport" , "Destination Airport" , "Carrier #" , "Price" , "Day" , "Time" , "Duration" , "Available Seats" };
JTable table = new JTable();
for ( int i = 0; i < columnNames.length; i++ ) {
table.setValueAt( columnNames[i] , 0, i);
}
When I execute it , I get the following error
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:417)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:276)
at javax.swing.JTable.convertColumnIndexToModel(JTable.java:1623)
at javax.swing.JTable.setValueAt(JTable.java:1737)
at suncertify.client.FBNClient.<init>(FBNClient.java:53)
at suncertify.client.FBNClient.main(FBNClient.java:94)
Something wrong with the row and column indexes used by me .
Any clues?
-- Ravindra
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is row number one based or zero based?
Mark
 
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well from what I see, the table yet doesnt know what columns it has. You will need to tell the table, thus the tablemodel as to what its column headers are. I think has not even created the column models. Typically a table is constructed with data and an array of column names to use. If you cant do that create a TableModel that extends AbstractTableModel and provide the functionality there. Good luck
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark
If I understood your question correctly? Right now , I am just trying to create a empty table then populate the top row ( 0th row ) with column names of the table.
Is that approach wrong ?
I'll try to implement the same using TableModel.
Thanks
-- Ravindra
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh , I see. ther is no row 0 they are not the headers. You need to set the headers via the setTableHeader method. However I think you will find all this much easier by extending AbstractTableModel, and creating this implementation of data and headers yourself. I even set mine from a DataInfo[] array passed to the constructor which passes the header names to an array, and also to set the data. Just look at what you can do with it.
Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic