Re: ResultSet next() throws Null Pointer Exception - Mailing list pgsql-general
From | captainmidgetdonkey@yahoo.com (Captain) |
---|---|
Subject | Re: ResultSet next() throws Null Pointer Exception |
Date | |
Msg-id | 3ccbfe01.0202191352.6206c137@posting.google.com Whole thread Raw |
In response to | ResultSet next() throws Null Pointer Exception (captainmidgetdonkey@yahoo.com (Captain)) |
List | pgsql-general |
captainmidgetdonkey@yahoo.com (Captain) wrote in message news:<3ccbfe01.0202181451.fb2d98a@posting.google.com>... > Hello. Any help would be gratefully appricated. > > I'm using Postgres 7.1.3. I have small test class. I have put a > comment line where this class fails. > > import java.sql.*; > > class DBTest { > > static { > try { > Class.forName("org.postgresql.Driver"); > } > catch (Exception e) { > System.err.println("Terminated. Could not load postgres > driver :" + e); > System.exit(1); > } > } > > private static Connection conn; > private static Statement stmt; > private static ResultSet rs; > private static String sql = "SELECT * FROM message"; > > > > public static void main(String[] args) { > if(args.length < 1) { > System.out.println("Usage: DBTest <database> [<sql > statement>]"); > System.exit(1); > } > if(args.length == 2) > sql = args[1]; > > try { > conn = DriverManager.getConnection("jdbc:postgresql:"+ > args[0],"user",""); > stmt = conn.createStatement(); > } > catch (SQLException sqle) { > System.err.println("Terminated. Could not create > connection or statement :" + sqle); > System.exit(1); > } > catch(Exception e) { > System.err.println("Terminated. Could not create > connection or statement :" + e); > System.exit(1); > } > try { > rs = stmt.executeQuery(sql); > } > catch (SQLException sqle) { > System.err.println("Terminated. Exception occured during > query execution (" + sql +") :" + sqle); > } > finally { > try { > rs.close(); > stmt.close(); > conn.close(); > } > catch (Exception e) { > System.exit(-1); > } > } > try { > ResultSetMetaData meta = rs.getMetaData(); > System.out.println("Number of column :" + > meta.getColumnCount()); > > if(rs.next()) { //THIS IS THE FAILURE LINE. EXCEPTION > THROWN IS NULL POINTER > System.out.println(rs.getString(1)); > } > /* > while(rs.next()) { > System.out.println(rs.getString(1) + " " + > rs.getString(2) + " " + rs.getString(3)); > } > */ > System.out.println("Ended without error"); > } > catch (SQLException sqle) { > System.err.println("Terminated. SQLException occured > during resultSet display :" + sqle); > sqle.printStackTrace(); > } > catch(Exception e) { > System.err.println("Terminated. Exception occured during > resultSet display :" + e); > e.printStackTrace(); > } > finally { > try { > rs.close(); > stmt.close(); > conn.close(); > } > catch (Exception e) { > System.exit(-1); > } > } > } > } > > When this class is run, this is the output. > > Number of column :6 > Terminated. Exception occured during resultSet display > :java.lang.NullPointerException > java.lang.NullPointerException > at org.postgresql.jdbc2.ResultSet.next(Unknown Source) > at com.ecmarket.util.database.test.DBTest.main(Unknown Source) > > What I know. The statement does return a ResultSet. The ResultSet is > not null when the rs.next() line is called. > > Can YOU see what I've missed or got wrong. Or maybe there is a bug in > the driver itself and you know the fix or workaround. > > Thanks ********** So, I'm a doofus! I have a finally block that closes my resultset before I call rs.next(). Thanks to anyone that did look at this, but without the finally block closing the resultset it works fine. Thanks, Captian. **********
pgsql-general by date: