简体   繁体   English

resultSet.next()抛出空指针异常

[英]resultSet.next() throwing null pointer exception

I'm working in java using the JDBC to perform queries on a database. 我在java中使用JDBC在数据库上执行查询。 For some reason, this code: 出于某种原因,这段代码:

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.... 虽然查询是正确的,显然我搞砸了我的字符串连接,因此它没有包含它应该的空间,并且它在测试时起作用,因为我是一个白痴并通过从代码中复制粘贴而不是拉实际变量来测试查询退出调试器....

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. 我的进程是一个长时间运行的进程,有200k行...我将使用一个特定的连接,并在完成该进程后丢弃它。

调试你的runSimpleQuery方法,它返回null,这就是为什么resultset.getNext()抛出NullPointerException

Have you checked connection object? 你检查过connection对象了吗? is it returning proper connection ? 是否恢复正常connection it might be possible that connection is null ..and it caught in catch block and returning null. 它可能是连接为null ..并且它在catch blockcatch block并返回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 你应该通过在catch块中打印exception来调试..所以你可能会知道这个null的确切原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM