ResultSet is not null but giving a null pointer exception

i have 16 to 17 record in DB BUT when i want to retrive data.... on the run time i received null pointer exceptiion (sorry for bad english)

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

Upvotes: 0

Views: 1210

Answers (2)

user207421
user207421

Reputation: 310884

If this is the real code you must be setting 'rs' to null later, inside the 'while' loop.

Upvotes: 0

Yasin
Yasin

Reputation: 2029

You cannot get NPE where you are pointing. while(rs.next()) this line will throw NPE if and only if the variable rs is null. And the javadoc says:

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

Hence, after rs=stmt.executeQuery(); the variable rs cannot be null. The source of the NPE could be somewhere else in the code but not the condition in while loop.

Upvotes: 1

Related Questions