gpt4 book ai didi

java - 调用 ResultSet.getDate() 时出现 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:18:32 26 4
gpt4 key购买 nike

我在以下代码中调用 ResultSet.getDate() 时收到了 java.lang.NullPointerException。但是,数据库中的条目似乎不是 null。连接似乎处于 Activity 状态,因为正在获取其他字段。我做错了什么?

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 |
+------------+----------+---------------------+

最佳答案

如本question所述,你能试试这样的东西吗;

我也从我上面提到的问题中提取了代码。

Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
date = new java.util.Date(timestamp.getTime()));

关于java - 调用 ResultSet.getDate() 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com