Re: ResultSet next() throws Null Pointer Exception

From: captainmidgetdonkey(at)yahoo(dot)com (Captain)
To: pgsql-general(at)postgresql(dot)org
Subject: Re: ResultSet next() throws Null Pointer Exception
Date: 2002-02-19 21:52:02
Message-ID: 3ccbfe01.0202191352.6206c137@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

captainmidgetdonkey(at)yahoo(dot)com (Captain) wrote in message news:<3ccbfe01(dot)0202181451(dot)fb2d98a(at)posting(dot)google(dot)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.
**********

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-02-19 21:52:36 Re:
Previous Message Bruce Momjian 2002-02-19 21:36:25 Re: Performance impact of NULLs and variable length fields