broliverparker
broliverparker

Reputation: 217

AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 error for JTable

I am trying to select date from my SQL database and put into a JTable, but I keep getting an error:

'AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0'

Not really sure why. This is my code:

table = new JTable();

try {
    Connection MyConn = null;
    Statement statement = null;
    ResultSet rs = null;

    Class.forName("com.mysql.jdbc.Driver");
    MyConn = DriverManager.getConnection("jdbc:mysql:"
            + "//localhost/eventbooking", "root", "Passw0rd");
    statement = MyConn.createStatement();
    rs = statement.executeQuery("SELECT * FROM `events` WHERE `Date Of Event` = '30/03/2014'");
    int i=0;
    while (rs.next()) {
        Object a = rs.getString("Date Of Event");
        Object b = rs.getString("Tickets Available");
        Object c = rs.getString("Price Of Event");
        Object d = rs.getString("Start Time");

        table.setRowSorter(null);
        table.getModel().setValueAt(a, i, 0 );
        table.getModel().setValueAt(b, i, 1);
        table.getModel().setValueAt(c, i, 2);
        table.getModel().setValueAt(d, i, 3);
        EventDiary.this.add(table);         
        System.out.println(i);
        i++;
    }

Upvotes: 0

Views: 5264

Answers (1)

t0mppa
t0mppa

Reputation: 4078

Problem is that you're creating a default JTable, which has a TableModel that consists of a Vector with no elements in it. Then you try to set the element at index 0 of said vector, which doesn't exist at this point and voilà: you face an issue.

For example:

JTable table = new JTable();
table.getModel().setValueAt("i", 0, 0);

System.out.println(table);

Results in:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
    at java.util.Vector.elementAt(Vector.java:470)
    at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:665)
    at com.stackoverflow.answer.JTableTest.main(JTableTest.java:9)

While the following for instance:

JTable table = new JTable(1, 1);
table.getModel().setValueAt("i", 0, 0);

System.out.println(table);

Results in:

javax.swing.JTable[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=251658568,maximumSize=,minimumSize=,preferredSize=,autoCreateColumnsFromModel=true,autoResizeMode=AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled=false,editingColumn=-1,editingRow=-1,gridColor=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],preferredViewportSize=java.awt.Dimension[width=450,height=400],rowHeight=16,rowMargin=1,rowSelectionAllowed=true,selectionBackground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=44,g=93,b=205],selectionForeground=com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255],showHorizontalLines=true,showVerticalLines=true]

Thus, if you want to persist with your way of doing things, you'd add in the rows and columns beforehand.

Another way to do it would be:

JTable table = new JTable();

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addColumn("id");
model.addColumn("name");

Object[] row = new Object[2];
row[0] = 1;
row[1] = "John";

model.addRow(row);

row = new Object[2];
row[0] = 2;
row[1] = "Jack";

model.addRow(row);

for (int i = 0; i < model.getRowCount(); i++) {
    for (int j = 0; j < model.getColumnCount(); j++) {
        System.out.println("column: " + model.getColumnName(j) + ", row #" + i + ", value: " + model.getValueAt(i, j));
    }
}

Resulting in:

column: id, row #0, value: 1
column: name, row #0, value: John
column: id, row #1, value: 2
column: name, row #1, value: Jack

Upvotes: 3

Related Questions