ResultSet next() throws Null Pointer Exception

From: captainmidgetdonkey(at)yahoo(dot)com (Captain)
To: pgsql-general(at)postgresql(dot)org
Subject: ResultSet next() throws Null Pointer Exception
Date: 2002-02-18 22:51:20
Message-ID: 3ccbfe01.0202181451.fb2d98a@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2002-02-19 00:59:14 Cannot restart the postmaster
Previous Message Vince Vielhaber 2002-02-18 22:35:44 Re: Can't get to interactive documentation