gpt4 book ai didi

java - ResultSet 不为 null 但给出空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:45:22 25 4
gpt4 key购买 nike

我在数据库中有 16 到 17 条记录但是当我想检索数据......在运行时我收到空指针异常(抱歉英语不好)

 sql="SELECT * FROM CUSTOMER ORDER BY date DESC"; //my query
stmt=con.prepareStatement(sql); //preparedStatement
System.out.println("2"); //checking output (where is exception)
rs=stmt.executeQuery(); //resultsetGlobal declare
System.out.println("3"); //checking outpur
i=0; //for show record number
System.out.println(i); //checking output
while(rs.next()){ //here i get the exception any one tell me why exception??
}

最佳答案

您无法在您所指向的位置获得 NPE。 while(rs.next()) 当且仅当变量 rs 为 null 时,此行才会抛出 NPE。还有javadoc说:

executeQuery() returns a ResultSet object that contains the data produced by the given query; never null`

因此,在rs=stmt.executeQuery();之后,变量rs不能为空。 NPE 的来源可能位于代码中的其他位置,但不是 while 循环中的条件。

关于java - ResultSet 不为 null 但给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756819/

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