postgresql resultSet.next() 抛出空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15722335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
resultSet.next() throwing null pointer exception
提问by The General
I'm working in java using the JDBC to perform queries on a database. For some reason, this code:
我在 java 中使用 JDBC 对数据库执行查询。出于某种原因,这段代码:
public static void checkAllFlights() {
String SQLStatement = "SELECT Flight.FlightID,"
+"(CASE WHEN (SUM(NumSeats) > 0) THEN SUM(NumSeats) ELSE 0 END)"
+"AS Booked_Seats,"
+"(CASE WHEN (MaxCapacity-SUM(NumSeats) > 0) THEN MaxCapacity-SUM(NumSeats) ELSE MaxCapacity END) AS Available_Seats,"
+"MaxCapacity"
+"FROM Flight LEFT JOIN FlightBooking ON Flight.FlightID = FlightBooking.FlightID"
+"GROUP BY Flight.FlightID, Flight.MaxCapacity"
+"ORDER BY Flight.FlightID";
try {
dbAccess.getConnection();
ResultSet resultSet = dbAccess.runSimpleQuery(SQLStatement);
System.out.println("FlightID "+"Booked_Seats "+"Available_Seats "+"Max_Capacity");
while (resultSet.next()) {
int ID = resultSet.getInt(1);
System.out.println(ID);
}
DBAccess.disconnect();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
is throwing me the error:
正在向我抛出错误:
java.lang.NullPointerException
at databases2.Databases2.checkAllFlights(Databases2.java:245)
at databases2.Databases2.main(Databases2.java:26)
(line "245" refers to the "while (resultSet.next())")
(行“245”指的是“while(resultSet.next())”)
resultSet.next() and resultSet.close() on their own also produce the same error.
resultSet.next() 和 resultSet.close() 本身也会产生同样的错误。
I've used essentially the same code further up to cycle over a resultset and it worked fine and I've run the SQL directly and that returns the correct results so I'm confused as to why the resultSet.next() could be throwing a null pointer exception?
我已经使用基本相同的代码进一步循环结果集,它运行良好,我直接运行 SQL 并返回正确的结果,所以我很困惑为什么 resultSet.next() 可能会抛出空指针异常?
The runSimpleQuery method:
runSimpleQuery 方法:
public ResultSet runSimpleQuery(String sql)
throws Exception {
try {
// Create a statement variable to be used for the sql query
statement = connection.createStatement();
// Perform the update
return statement.executeQuery(sql);
} catch (SQLException e) {
// Problem encountered
statement = null;
return null;
}
Update: After stepping through it, it does appear that runSimpleQuery is returning null which is something it shouldn't be doing unless an sql exception is throw....
更新:在逐步完成之后,runSimpleQuery 似乎确实返回了 null,这是它不应该做的事情,除非抛出 sql 异常......
Update:Solved. Whilst the query was right, apparently I messed up my string concatenation so that it didn't include spaces where it should and it worked when testing because I was an idiot and tested the query by copy-pasting from code rather than pulling the actual variable out of the debugger....
更新:已解决。虽然查询是正确的,但显然我弄乱了我的字符串连接,因此它不包含它应该的空格并且在测试时它可以工作,因为我是一个白痴并通过从代码中复制粘贴而不是提取实际变量来测试查询出调试器....
回答by Rodrigo Asensio
I ran into the same issue and I discovered that if other thread uses the same connection the ResultSet will throw a NullPointerException after the other sql is executed.
我遇到了同样的问题,我发现如果其他线程使用相同的连接,则 ResultSet 将在执行其他 sql 后抛出 NullPointerException。
sys.scheduler 06:28:00,017 [SchedulerReloader] ERROR - java.lang.NullPointerException
sys.scheduler 06:28:00,018 [SchedulerReloader] ERROR - com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7009)
My process is a long running process with 200k rows... I will use a particular connection for this and discard it after the process is done.
我的进程是一个长时间运行的进程,有 20 万行……我将为此使用一个特定的连接,并在进程完成后丢弃它。
回答by Vishal K
Have you checked connection
object? is it returning proper connection
? it might be possible that connection is null
..and it caught in catch block
and returning null. You should debug that .. by printing the exception
in catch block ..so that you may come to know the exact reason for this null
你检查过connection
对象吗?它返回正确connection
吗?连接可能是null
.. 并且它被捕获catch block
并返回空值。您应该通过打印exception
in catch 块来调试 ..这样您就可以知道发生这种情况的确切原因null
回答by Pavel
Debug your runSimpleQuery method, it returns null, that's why resultset.getNext() throw NullPointerException
调试你的 runSimpleQuery 方法,它返回 null,这就是为什么 resultset.getNext() 抛出 NullPointerException