2

我正在使用 JDBC 在 java 中对数据库执行查询。出于某种原因,这段代码:

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);
    } 
}

正在向我抛出错误:

java.lang.NullPointerException
        at databases2.Databases2.checkAllFlights(Databases2.java:245)
        at databases2.Databases2.main(Databases2.java:26)

(“245”行指的是“while (resultSet.next())”)

resultSet.next() 和 resultSet.close() 本身也会产生相同的错误。

我已经使用基本相同的代码进一步循环结果集,它工作正常,我直接运行 SQL 并返回正确的结果,所以我很困惑为什么 resultSet.next() 可能会抛出空指针异常?

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;
    }

更新:在逐步完成之后,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....

4

3 回答 3

3

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.

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.

于 2013-09-19T10:40:22.377 回答
2

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

于 2013-03-30T19:56:07.307 回答
1

Debug your runSimpleQuery method, it returns null, that's why resultset.getNext() throw NullPointerException

于 2013-03-30T19:13:11.097 回答