-1

I am trying to fill a Object[][] array with Object[]like you would add rows to a table. But all I get is a java.lang.ArrayStoreException at the line data[resultSet.getRow() - 1]= mailbox;. Whare am I worng? The SQL part is working

public static Object[][] getMailboxes() {
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    Object[][] data = null;
    try {
        statement = conn.prepareStatement(GET_MAILBOX, ResultSet.TYPE_SCROLL_INSENSITIVE,
                CONCUR_READ_ONLY);
        statement.execute();
        resultSet = statement.getResultSet();
        ResultSetMetaData rsmd = resultSet.getMetaData();
        resultSet.last();
        data = new String[resultSet.getRow()][7];
        resultSet.beforeFirst();
        Object[] mailbox = new Object[7];
        while (resultSet.next()) {
            for (int i = 0; i < 7; i++) {
                System.out.println(i+" column of row "+resultSet.getRow());
                if (0 == i) {
                    mailbox[i] = resultSet.getInt(i + 1);
                } else if (6 == i || 5 == i) {
                    mailbox[i] = resultSet.getBoolean(i + 1);
                } else {
                    mailbox[i] = resultSet.getObject(i + 1);
                }
            }
            data[resultSet.getRow() - 1]= mailbox;
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return data;
} 
Albritter
  • 33
  • 1
  • 6
  • You have defined `data` as an array of strings. Therefore, you cannot store an Int or Boolean into it – jr593 Jun 29 '16 at 11:52

2 Answers2

0

Change

data = new String[resultSet.getRow()][7];

to

data = new Object[resultSet.getRow()][7];

since you are attempting to store in this array objects which are not Strings, so they can't be stored in a String[][] array.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

From the docs ArrayStoreException it seems like you are storing the wrong type into your array variable. Try to Declare your array as an array of Strings, i. e.

String[][] data = null;

Apart from that, you could move the return statement into your try block in order to declare your result variables inside that block. If your try block fails, you can simply return null anyway.

Smoof
  • 1
  • 1