9

I am getting a java.lang.NullPointerException on calling ResultSet.getDate() in the following code. The entry in the database, however, doesn't seem to be null. The connection seems to be active, since other fields are being fetched. What am I doing wrong?

try {
    ... /* Code that creates a connection and initializes statement */

    String query = "SELECT * FROM groups WHERE id = 'testGroup1'";
    ResultSet rs = statement.executeQuery(query);
    if(rs.next()) {
        admin = rs.getString("admin_id");
        User.process(admin);
        java.sql.Date created_on = rs.getDate("created_on");
        System.out.println("Created on = " + created_on.toString());
    }
}
catch(Exception e) {
    System.out.println("Stuck here");
    e.printStackTrace();
}

Here's the output and the stack trace:

Admin id = 42          // User.process prints the admin id
Stuck here
java.lang.NullPointerException
    at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:966)
    at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:1988)
    at com.myapp.server.model.Group.initInfo(Group.java:39)
    ...

I have the following schema:

+-------------------+--------------+------+-----+---------+-------+
| Field             | Type         | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| id                | varchar(50)  | NO   | PRI | NULL    |       |
| admin_id          | varchar(50)  | NO   | MUL | NULL    |       |
| created_on        | datetime     | NO   |     | NULL    |       |
+-------------------+--------------+------+-----+---------+-------+

And the following entry in the database:

+------------+----------+---------------------+
| id         | admin_id | created_on          |
+------------+----------+---------------------+
| testGroup1 | 42       | 2014-12-15 22:46:31 |
+------------+----------+---------------------+
7
  • 2
    Could you try rs.getTimestamp();
    – Semih Eker
    Dec 15, 2014 at 17:30
  • I think the issue will be with the SQL date format. Is the datetime a proper datatype you are referring? Try Date instead. Dec 15, 2014 at 17:31
  • @SemihEker getTimestamp() also throws NullPointerException.
    – John Bupit
    Dec 15, 2014 at 17:34
  • 2
    It seems like a bug in your jdbc driver. What is the version you are using? Recent mysql drivers do not even have a class called ResultSet
    – Dima
    Dec 15, 2014 at 17:37
  • @Dima I was using mysql-connector-java-5.0.8. I've upgraded to mysql-connector-java-5.1.34 which does seem to have a ResultSet class. It now shows a java.sql.SQLException saying Operation not allowed after ResultSet closed.
    – John Bupit
    Dec 15, 2014 at 17:47

3 Answers 3

4

As mentioned in this question,could you try sthg like that;

I take the code also from the question I mentioned above.

Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
    date = new java.util.Date(timestamp.getTime()));
3
  • getTimestamp() also gives the same error, like I mentioned in the comment. Getting a NullPointerException on rs.getTimestamp("created_on").
    – John Bupit
    Dec 15, 2014 at 17:54
  • You said that "Like @SemihEker suggested, getTimestamp() eventually gives me the desired result. ". Is this answer ok for you?
    – Semih Eker
    Dec 15, 2014 at 18:13
  • Yes, once I get rid of the NPE. The error was in the User.process function. Thanks.
    – John Bupit
    Dec 15, 2014 at 18:14
3

The Type of the field created_onin your database is datetime, this is not the same as the Java Date

You'll need to convert it.

If you look at the first answer at this question you'll see how to fix it

2
  • Thanks for the link. Using getTimestamp() gives me the desired result, once I get rid of the NullPointerException.
    – John Bupit
    Dec 15, 2014 at 18:08
  • The conversion from a datetime (equivalent to a timestamp) is automatic and required by the JDBC specification (it will simply drop the time) Dec 16, 2014 at 13:11
2

I found User.process further queried the database thereby closing the current ResultSet (or so I speculate). The NullPointerException goes away when I do the following:

try {
    ...
    if(rs.next()) {
        admin = rs.getString("admin_id");
        java.sql.Timestamp created_on = rs.getTimestamp("created_on");
        User.process(admin);
    }
}
catch(Exception e) {
    ...
}

Also using getTimestamp() as suggested by other solutions gets me the desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.