pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedfirst() causes NullPointerException
3+ messages / 2 participants
[nested] [flat]
* first() causes NullPointerException
@ 2004-12-23 09:58 Nico <nicohmail-postgresql@yahoo.com>
0 siblings, 1 reply; 3+ messages in thread
From: Nico @ 2004-12-23 09:58 UTC (permalink / raw)
To: pgsql-jdbc
I have a servlet that extracts data from a postgresql database and makes a
nice menu of it. The connection works fine, I can execute my sql-statement
without any problems, but when I try the first() method of ResultSet it
fails: it throws a NullPointerException at line 216.
This are the first lines of the exception:
java.lang.NullPointerException
at
org.postgresql.jdbc2.AbstractJdbc2ResultSet.first(AbstractJdbc2ResultSet.java:216)
at menus.DBMenu.Menupage(DBMenu.java:68)
at menus.DBMenuFind.doGet(DBMenuFind.java:103)
Here is the java source code I used for the connection and resultset:
protected static ResultSet getQuery(String strstatement) throws
SQLException
{
ResultSet resultset=statement.executeQuery(strstatement);
if(resultset==null)
throw new SQLException("Resultset is null at getQuery().");
if(resultset.getWarnings()==null)
return resultset;
else
{
String errmessage="";
while(resultset.getWarnings().getErrorCode()!=0)
{
errmessage+=resultset.getWarnings().getMessage()+"\n";
resultset.getWarnings().getNextException();
}
throw new SQLException("SQL Fout getQuery(): "+errmessage);
}
}
protected final static void Menupage(String app)
throws MenuException, DriverNotFoundException
{
//resultsetL1 and resultsetL2 are queries that extract data from
views in the db and resultsetL1C and
//resultsetL2C are the queries that count the rows in those queries.
ResultSet resultsetL1,resultsetL2,resultsetL1C,resultsetL2C;
try
{
connect(strDbdriver);
resultsetL1=getQuery("SELECT * FROM \"qryMenuL1\" ORDER BY "+
"\"MenuL1Order\"");
resultsetL1C=getQuery("SELECT COUNT(\"MenuL1ID\") FROM
\"qryMenuL1\"");
if(resultsetL1==null)
throw new MenuException("ResultsetL1 is null for unknown
reasons.");
if(resultsetL1C==null)
throw new MenuException("ResultsetL1C is null for unknown
reasons.");
resultsetL1C.first();
if(resultsetL1C.getInt(1)==0)
throw new MenuException("Menu error: no data found.");
resultsetL1.first();
//following lines are irrelevant since the exception occurs at first().
}
protected static void connect(String dbdriver)
throws DriverNotFoundException
{
String connprob="";
try
{
connprob="driver problem:"+dbdriver;
Class.forName(dbdriver);
connprob="connection problem:
jdbc:postgresql://localhost/"+dbname+
"?user=username&password=password";
connection =
DriverManager.getConnection("jdbc:postgresql://localhost/" +
dbname + "?user=username&password=password");
connprob="statement problem";
statement = connection.createStatement();
}
catch(ClassNotFoundException exception)
{
throw new DriverNotFoundException(dbdriver);
}
catch(Exception exception)
{
exception.printStackTrace();
throw new DriverNotFoundException("Connection problem:
"+exception.getMessage()+"<br>"+connprob);
}
}
As you could see, I changed the username & password for security reasons,
any SQL syntax error is captured before the first() method is called. And if
the executeQuery() method returns null which is normally impossible
according to the java docs, it is captured, too. I have tried everything.
Does anybody got any ideas?
Nico.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: first() causes NullPointerException
@ 2004-12-23 10:30 Kris Jurka <books@ejurka.com>
parent: Nico <nicohmail-postgresql@yahoo.com>
0 siblings, 1 reply; 3+ messages in thread
From: Kris Jurka @ 2004-12-23 10:30 UTC (permalink / raw)
To: Nico <nicohmail-postgresql@yahoo.com>; +Cc: pgsql-jdbc
On Thu, 23 Dec 2004, Nico wrote:
> I have a servlet that extracts data from a postgresql database and makes a
> nice menu of it. The connection works fine, I can execute my sql-statement
> without any problems, but when I try the first() method of ResultSet it
> fails: it throws a NullPointerException at line 216.
>
> resultsetL1=getQuery("SELECT * FROM \"qryMenuL1\" ORDER BY "+
> "\"MenuL1Order\"");
> resultsetL1C=getQuery("SELECT COUNT(\"MenuL1ID\") FROM
> \"qryMenuL1\"");
According to the JDBC spec you are only allowed to have one ResultSet open
per Statement. When you create the second ResultSet the first is
automatically closed. This causes the NPE or in the CVS version of the
driver an error message indicating that the result is already closed.
Kris Jurka
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: first() causes NullPointerException
@ 2004-12-23 11:06 Nico <nicohmail-postgresql@yahoo.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 0 replies; 3+ messages in thread
From: Nico @ 2004-12-23 11:06 UTC (permalink / raw)
To: pgsql-jdbc
Thanks. I will try it right away.
Nico.
"Kris Jurka" <books@ejurka.com> schreef in bericht
news:Pine.BSO.4.56.0412230526470.22886@leary.csoft.net...
>
>
> On Thu, 23 Dec 2004, Nico wrote:
>
>> I have a servlet that extracts data from a postgresql database and makes
>> a
>> nice menu of it. The connection works fine, I can execute my
>> sql-statement
>> without any problems, but when I try the first() method of ResultSet it
>> fails: it throws a NullPointerException at line 216.
>>
>> resultsetL1=getQuery("SELECT * FROM \"qryMenuL1\" ORDER BY "+
>> "\"MenuL1Order\"");
>> resultsetL1C=getQuery("SELECT COUNT(\"MenuL1ID\") FROM
>> \"qryMenuL1\"");
>
> According to the JDBC spec you are only allowed to have one ResultSet open
> per Statement. When you create the second ResultSet the first is
> automatically closed. This causes the NPE or in the CVS version of the
> driver an error message indicating that the result is already closed.
>
> Kris Jurka
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2004-12-23 11:06 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-23 09:58 first() causes NullPointerException Nico <nicohmail-postgresql@yahoo.com>
2004-12-23 10:30 ` Kris Jurka <books@ejurka.com>
2004-12-23 11:06 ` Nico <nicohmail-postgresql@yahoo.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox