在以下代码中调用ResultSet.getDate()
时,我收到了一个java.lang.NullPointerException
。然而,数据库中的条目似乎并不是null
。由于其他字段被获取,连接似乎是活动的。我做错了什么?
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();
}
这是输出和堆栈跟踪信息:
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)
...
我有以下模式:
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| id | varchar(50) | NO | PRI | NULL | |
| admin_id | varchar(50) | NO | MUL | NULL | |
| created_on | datetime | NO | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
以下是数据库中的条目:
+------------+----------+---------------------+
| id | admin_id | created_on |
+------------+----------+---------------------+
| testGroup1 | 42 | 2014-12-15 22:46:31 |
+------------+----------+---------------------+
datetime
是合适的数据类型吗?试试用Date
代替。 - Aditya PeshavegetTimestamp()
方法也会抛出NullPointerException
异常。 - John BupitgetTimestamp()
最终给了我想要的结果。 - John Bupit