Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NullPointerException when traversing a non-null recordset

I am running a query on Sybase ASE that produces a ResultSet that I then traverse and write the contents out to a file. Sometimes, this will throw a NullPointerException, stating that the ResultSet is null. However, it will do this after printing out one or two records. Other times, with the same exact input, I will receive no errors.

I have been unable to consistently produce this error. The error message is pointing to a line:

output.print(rs.getString(1));

It appears to happen when the query takes a little longer to run, for some reason. The recordset returns thus far have been very small (4 to 7 records). Sometimes I'll have to run the app 3 or 4 times, then the errors will just stop, as though the query was getting "warmed up". I've run the query manually and there doesn't appear to be any performance problems.

Thanks again!


1 Answers

Are you sure it's the ResultSet that's null, and not the rs.getString(1)?

This is a typical Java query

preparedStatement.setLong(1, primaryKey);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next())
{
  String foo = rs.getString(1);
  if (foo != null)
    ...
}
like image 65
Paul Tomblin Avatar answered Dec 06 '25 22:12

Paul Tomblin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!