-2

I have written a method that will return an Object[][] to use as the data for a JTable. I read the data in from a Microsoft Access Database and then put each piece of information into a cell in the Object [][]. My problem is the return statement of my method which gives me a RuntimeException. When I use a System.out.println() to print out each element of the object all the data is there and it works fine, but when it gets to the returning of that object then it falls through.

public Object [] [] AllWorldBestTimes() throws SQLException
{
  DatabaseConnection connection = new DatabaseConnection();

  ResultSet result = connection.SelectStatements("SELECT * FROM WorldBestTimes");

  count = 24;

  Object [] [] data = new String[count][4];    

    int row = 0;

    while(row < count)
    {
        data [row][0] = result.getString(1); 
        data [row][1] = result.getString(2);
        data [row][2] = result.getString(3);
        data [row][3] = result.getString(4);
        result.next();  

        System.out.println(data [row][0]);
        System.out.println(data [row][1]);
        System.out.println(data [row][2]);
        System.out.println(data [row][3]);

        row++;
    }

    connection.close();

    return data;
}

The above code displays the exact data that needs to be returned when it is printed out but returning it gives me a RuntimeException.

UPDATE

Stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 1
    'returning it gives me a NullPointerException' does not make sense. Please show the stacktrace – home Jun 23 '13 at 10:27
  • Returning it? Or maybe when printing out that data? – Igor Rodriguez Jun 23 '13 at 10:27
  • Maybe try to append to `""` String or check if `getString()` is `null` so you can throw NPE yourself? – tkroman Jun 23 '13 at 10:31
  • 1
    @IgorRodriguez Absolutely **not**. if a string (or an object in general) is null and you print it, then it will print `null`, if you **try to use** a null object, it will throw a `NullPointerException`, but printing a null object doesn't throw it – BackSlash Jun 23 '13 at 10:31
  • but when i print it out all the data is there. its only returning it that i get the problem. – user2513354 Jun 23 '13 at 10:33
  • 2
    @user2513354 **Where is the stack trace?** – Ingo Jun 23 '13 at 10:34
  • 2
    @user2513354 **Post the full stack trace**. Without it we can only guess what is the error, but we can't be sure if you don't post the stack trace – BackSlash Jun 23 '13 at 10:34
  • The actual object[][] has all the data it needs to because i can print it out just fine. I ran a debugger and it gives the nullpointerexception on the return statement – user2513354 Jun 23 '13 at 10:35
  • Whats is the stacktrace? – user2513354 Jun 23 '13 at 10:35
  • @user2513354 The stacktrace is the line that says something like `Exception in thread main: java.lang.NullPointerException at .......` – BackSlash Jun 23 '13 at 10:36
  • Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown – user2513354 Jun 23 '13 at 10:40
  • 3
    @user2513354 Why did you say `NullPointerException`? – BackSlash Jun 23 '13 at 10:49

2 Answers2

4

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown

This means you are running code which doesn't compile. Some IDE's allow this such as eclipse and this is a VERY BAD IDEA in my opinion as it means you maximise the cost of finding and fixing those errors. I suggest you turn this feature off.

java.lang.RuntimeException: Uncompilable source code - what can cause this?


I would write this code very different if you can. Opening and closing a database connection is very expensive. You can use a List<String[]> to read all the rows instead of assuming there is 24.

public String[][] AllWorldBestTimes() throws SQLException {
    return select("SELECT * FROM WorldBestTimes");
}

public String[][] select(String selectSQL) throws SQLException {
    // TODO use a connection pool instead.
    Connection connection = new DatabaseConnection();
    List<String[]> data;
    PreparedStatement statement = null;
    ResultSet result = null;
    try {
        statement = connection.prepareStatement(selectSQL);
        result = statement.executeQuery();
        data = new ArrayList<String[]>();
        int columnCount = result.getMetaData().getColumnCount();
        while (result.next()) {
            String[] row = new String[columnCount];
            for (int i = 0; i < columnCount; i++)
                row[i] = result.getString(i + 1);
            data.add(row);
        }
    } finally {
        if (result != null) {
            result.close();
        }
        if (statement != null) {
            statement.close();
        }
        connection.close();
    }
    return data.toArray(new String[data.size()][]);

    }
}
Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • The number of rows might actually be the cause of the error. Suppose there are less than 24. – Ingo Jun 23 '13 at 10:35
  • @Ingo I thought of that, but I wouldn't expect result.getString() or result.next() to throw an NPE, an SQLException would make more sense. Perhaps the OP is not printing the error correctly. – Peter Lawrey Jun 23 '13 at 10:37
  • 1
    Well, in fact it is an SQLException, the OP written it wrong. – BackSlash Jun 23 '13 at 10:48
  • 2
    He's not actually getting an `SQLException`. He's getting a compilation error because he does not catch or declare an `SQLException` somewhere and then he's getting an "Uncompilable source code" exception because he's running code that did not fully compile. – sepp2k Jun 23 '13 at 10:57
  • @sepp2k Thank you, I have updated my answer. It amazes me that people inflict this feature on themselves willingly. – Peter Lawrey Jun 23 '13 at 11:33
2

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown

This error message indicates that you are running a partially compiled program and the part that failed to compile, did so because it called a method that may throw an SQLException without either catching that exception or declaring throws SQLException. Since the given method actually does declare throws SQLException, I assume that the error is actually in a different method (probably the one that calls this one).

Note that this excpetion does not indicate that any SQLException is actually thrown - only that it might be thrown and you don't account for that possibility.

sepp2k
  • 363,768
  • 54
  • 674
  • 675