Thread: NullPointer error returned from ResultSet.java

NullPointer error returned from ResultSet.java

From
Jeff Duffy
Date:
Greetings,

 I have a JSP page with a rather large scriptlet that uses multiple nested
statements and ResultSets. Under very specific conditions I am encountering
the following error:

java.lang.NullPointerException
 at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)


Here's one pertinent snippet of my code:
( This code begins inside a while(rs.next()) loop for ResultSet rs,
using Statement stmt)


sql.append("AN SQL QUERY");
rs2 = stmt2.executeQuery(sql.toString());

while(rs2.next()){
     String courseId = rs2.getString(1);
     String roleName = rs2.getString(2);

     rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
     rs3.next();
     assignmentId = rs3.getString(1);

    sql.delete(0, sql.length());
    sql.append("AN SQL QUERY");
    stmt3.executeUpdate(sql.toString());
}

 Through debugging it appears that rs2 is throwing the error; the message
states the error is in method next(), but if I have only zero records or
only one record in rs2, there is no error. It's only when I have multiple
records that the error is thrown the second time round the while loop, so I
suspect it may be thrown at the assignment of courseId.

 This error is reproduced given the same conditions (nested ResultSets with
the innermost ResultSet executing an Update inside a while(ResultSet.next())
loop.  It almost seems that the call to rs3.executeUpdate() is closing rs2,
but I cannot see why that would happen. This is occurring in at least six
different source files.

 If I create a new Vector and stuff the contents of rs2 into it, then
iterate over the vector and use rs3 as shown above, no error is thrown.

 Updating to the 7.3beta1 JDBC driver changes the error message to:

javax.servlet.ServletException: postgresql.con.closed

 which is even more puzzling.


 Notes about my coding style that may be relevant:
- I use one StringBuffer for all sql queries and clear it for each new query
for efficiency. This hasn't ever caused problems elsewhere.

- I predeclare all Statement and ResultSet objects at the outermost block of
code (the 'top') like so:

Statement stmt   = conn.createStatement();
Statement stmt2  = conn.createStatement();
Statement stmt3  = conn.createStatement();
ResultSet rs, rs2, rs3;

Other data:

- The connections are obtained from a custom class that uses the jdbcpool
pool manager.
- I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
- The JDBC library is v7.2 (the pgjdbc2.jar binary download).
- PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96

 Any flashes of insight?

Thanks

Jeff Duffy
jeff@alanne.com


Re: NullPointer error returned from ResultSet.java

From
Dave Cramer
Date:
Jeff,

You can't have two result sets open on the same connection.

Dave
On Mon, 2002-10-07 at 11:57, Jeff Duffy wrote:
> Greetings,
>
>  I have a JSP page with a rather large scriptlet that uses multiple nested
> statements and ResultSets. Under very specific conditions I am encountering
> the following error:
>
> java.lang.NullPointerException
>  at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
>
>
> Here's one pertinent snippet of my code:
> ( This code begins inside a while(rs.next()) loop for ResultSet rs,
> using Statement stmt)
>
>
> sql.append("AN SQL QUERY");
> rs2 = stmt2.executeQuery(sql.toString());
>
> while(rs2.next()){
>      String courseId = rs2.getString(1);
>      String roleName = rs2.getString(2);
>
>      rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
>      rs3.next();
>      assignmentId = rs3.getString(1);
>
>     sql.delete(0, sql.length());
>     sql.append("AN SQL QUERY");
>     stmt3.executeUpdate(sql.toString());
> }
>
>  Through debugging it appears that rs2 is throwing the error; the message
> states the error is in method next(), but if I have only zero records or
> only one record in rs2, there is no error. It's only when I have multiple
> records that the error is thrown the second time round the while loop, so I
> suspect it may be thrown at the assignment of courseId.
>
>  This error is reproduced given the same conditions (nested ResultSets with
> the innermost ResultSet executing an Update inside a while(ResultSet.next())
> loop.  It almost seems that the call to rs3.executeUpdate() is closing rs2,
> but I cannot see why that would happen. This is occurring in at least six
> different source files.
>
>  If I create a new Vector and stuff the contents of rs2 into it, then
> iterate over the vector and use rs3 as shown above, no error is thrown.
>
>  Updating to the 7.3beta1 JDBC driver changes the error message to:
>
> javax.servlet.ServletException: postgresql.con.closed
>
>  which is even more puzzling.
>
>
>  Notes about my coding style that may be relevant:
> - I use one StringBuffer for all sql queries and clear it for each new query
> for efficiency. This hasn't ever caused problems elsewhere.
>
> - I predeclare all Statement and ResultSet objects at the outermost block of
> code (the 'top') like so:
>
> Statement stmt   = conn.createStatement();
> Statement stmt2  = conn.createStatement();
> Statement stmt3  = conn.createStatement();
> ResultSet rs, rs2, rs3;
>
> Other data:
>
> - The connections are obtained from a custom class that uses the jdbcpool
> pool manager.
> - I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
> - The JDBC library is v7.2 (the pgjdbc2.jar binary download).
> - PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
>
>  Any flashes of insight?
>
> Thanks
>
> Jeff Duffy
> jeff@alanne.com
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
>




Re: NullPointer error returned from ResultSet.java

From
"G.Nagarajan"
Date:
Hi,
I am also getting the same error. In my case I am using a prepared statement
to create the resultset like

  PreparedStatemet stmt = "...";

  callerFunction()
  {
    createPreparedStatement();
    fun1();
    closePreparedStatement();
  }
  fun1()
  {
     Results rs = stmt.executeQuery();

    while (rs.next() )
    {
        fun1(); //recursive call
      }
      rs.close();
  }

The null pointer exception is throws after three recursive calls. The code
at
113 is rows.size(). Perhaps it has something to do with caching? The error
doesn't come if I use normal statements.

Regards,
Nagarajan.


> -----Original Message-----
> From: pgsql-jdbc-owner@postgresql.org
> [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Jeff Duffy
> Sent: Monday, October 07, 2002 5:57 PM
> To: pgsql-jdbc@postgresql.org
> Subject: [JDBC] NullPointer error returned from ResultSet.java
>
>
> Greetings,
>
>  I have a JSP page with a rather large scriptlet that uses multiple nested
> statements and ResultSets. Under very specific conditions I am
> encountering
> the following error:
>
> java.lang.NullPointerException
>  at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
>
>
> Here's one pertinent snippet of my code:
> ( This code begins inside a while(rs.next()) loop for ResultSet rs,
> using Statement stmt)
>
>
> sql.append("AN SQL QUERY");
> rs2 = stmt2.executeQuery(sql.toString());
>
> while(rs2.next()){
>      String courseId = rs2.getString(1);
>      String roleName = rs2.getString(2);
>
>      rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
>      rs3.next();
>      assignmentId = rs3.getString(1);
>
>     sql.delete(0, sql.length());
>     sql.append("AN SQL QUERY");
>     stmt3.executeUpdate(sql.toString());
> }
>
>  Through debugging it appears that rs2 is throwing the error; the message
> states the error is in method next(), but if I have only zero records or
> only one record in rs2, there is no error. It's only when I have multiple
> records that the error is thrown the second time round the while
> loop, so I
> suspect it may be thrown at the assignment of courseId.
>
>  This error is reproduced given the same conditions (nested
> ResultSets with
> the innermost ResultSet executing an Update inside a
> while(ResultSet.next())
> loop.  It almost seems that the call to rs3.executeUpdate() is
> closing rs2,
> but I cannot see why that would happen. This is occurring in at least six
> different source files.
>
>  If I create a new Vector and stuff the contents of rs2 into it, then
> iterate over the vector and use rs3 as shown above, no error is thrown.
>
>  Updating to the 7.3beta1 JDBC driver changes the error message to:
>
> javax.servlet.ServletException: postgresql.con.closed
>
>  which is even more puzzling.
>
>
>  Notes about my coding style that may be relevant:
> - I use one StringBuffer for all sql queries and clear it for
> each new query
> for efficiency. This hasn't ever caused problems elsewhere.
>
> - I predeclare all Statement and ResultSet objects at the
> outermost block of
> code (the 'top') like so:
>
> Statement stmt   = conn.createStatement();
> Statement stmt2  = conn.createStatement();
> Statement stmt3  = conn.createStatement();
> ResultSet rs, rs2, rs3;
>
> Other data:
>
> - The connections are obtained from a custom class that uses the jdbcpool
> pool manager.
> - I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
> - The JDBC library is v7.2 (the pgjdbc2.jar binary download).
> - PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
>
>  Any flashes of insight?
>
> Thanks
>
> Jeff Duffy
> jeff@alanne.com
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org


Re: NullPointer error returned from ResultSet.java

From
Barry Lind
Date:
Nagarajan,

If you submit a complete test case that demonstrates the problem I would
be willing to look into this.  But there just isn't enough information
in what you have provided here to debug this.  I don't even know the
exact error you are getting.

thanks,
--Barry



G.Nagarajan wrote:
 > Hi,
 > I am also getting the same error. In my case I am using a prepared
statement
 > to create the resultset like
 >
 >   PreparedStatemet stmt = "...";
 >
 >   callerFunction()
 >   {
 >     createPreparedStatement();
 >     fun1();
 >     closePreparedStatement();
 >   }
 >   fun1()
 >   {
 >      Results rs = stmt.executeQuery();
 >
 >     while (rs.next() )
 >     {
 >         fun1(); //recursive call
 >       }
 >       rs.close();
 >   }
 >
 > The null pointer exception is throws after three recursive calls. The
code
 > at
 > 113 is rows.size(). Perhaps it has something to do with caching? The
error
 > doesn't come if I use normal statements.
 >
 > Regards,
 > Nagarajan.
 >
 >
 >
 >>-----Original Message-----
 >>From: pgsql-jdbc-owner@postgresql.org
 >>[mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Jeff Duffy
 >>Sent: Monday, October 07, 2002 5:57 PM
 >>To: pgsql-jdbc@postgresql.org
 >>Subject: [JDBC] NullPointer error returned from ResultSet.java
 >>
 >>
 >>Greetings,
 >>
 >> I have a JSP page with a rather large scriptlet that uses multiple
nested
 >>statements and ResultSets. Under very specific conditions I am
 >>encountering
 >>the following error:
 >>
 >>java.lang.NullPointerException
 >> at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
 >>
 >>
 >>Here's one pertinent snippet of my code:
 >>( This code begins inside a while(rs.next()) loop for ResultSet rs,
 >>using Statement stmt)
 >>
 >>
 >>sql.append("AN SQL QUERY");
 >>rs2 = stmt2.executeQuery(sql.toString());
 >>
 >>while(rs2.next()){
 >>     String courseId = rs2.getString(1);
 >>     String roleName = rs2.getString(2);
 >>
 >>     rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
 >>     rs3.next();
 >>     assignmentId = rs3.getString(1);
 >>
 >>    sql.delete(0, sql.length());
 >>    sql.append("AN SQL QUERY");
 >>    stmt3.executeUpdate(sql.toString());
 >>}
 >>
 >> Through debugging it appears that rs2 is throwing the error; the message
 >>states the error is in method next(), but if I have only zero records or
 >>only one record in rs2, there is no error. It's only when I have multiple
 >>records that the error is thrown the second time round the while
 >>loop, so I
 >>suspect it may be thrown at the assignment of courseId.
 >>
 >> This error is reproduced given the same conditions (nested
 >>ResultSets with
 >>the innermost ResultSet executing an Update inside a
 >>while(ResultSet.next())
 >>loop.  It almost seems that the call to rs3.executeUpdate() is
 >>closing rs2,
 >>but I cannot see why that would happen. This is occurring in at least six
 >>different source files.
 >>
 >> If I create a new Vector and stuff the contents of rs2 into it, then
 >>iterate over the vector and use rs3 as shown above, no error is thrown.
 >>
 >> Updating to the 7.3beta1 JDBC driver changes the error message to:
 >>
 >>javax.servlet.ServletException: postgresql.con.closed
 >>
 >> which is even more puzzling.
 >>
 >>
 >> Notes about my coding style that may be relevant:
 >>- I use one StringBuffer for all sql queries and clear it for
 >>each new query
 >>for efficiency. This hasn't ever caused problems elsewhere.
 >>
 >>- I predeclare all Statement and ResultSet objects at the
 >>outermost block of
 >>code (the 'top') like so:
 >>
 >>Statement stmt   = conn.createStatement();
 >>Statement stmt2  = conn.createStatement();
 >>Statement stmt3  = conn.createStatement();
 >>ResultSet rs, rs2, rs3;
 >>
 >>Other data:
 >>
 >>- The connections are obtained from a custom class that uses the jdbcpool
 >>pool manager.
 >>- I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
 >>- The JDBC library is v7.2 (the pgjdbc2.jar binary download).
 >>- PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
 >>
 >> Any flashes of insight?
 >>
 >>Thanks
 >>
 >>Jeff Duffy
 >>jeff@alanne.com
 >>
 >>
 >>---------------------------(end of broadcast)---------------------------
 >>TIP 6: Have you searched our list archives?
 >>
 >>http://archives.postgresql.org
 >
 >
 >
 > ---------------------------(end of broadcast)---------------------------
 > TIP 6: Have you searched our list archives?
 >
 > http://archives.postgresql.org
 >




Re: NullPointer error returned from ResultSet.java

From
"G.Nagarajan"
Date:
Hi Barry,
You had already answered the question, sorry for not posting a follow up:).

Since the function calls itself, there will more than one resultset open. I
modified
the code to get the list into a vector before recursing. Then I used the
vector
rather than moving through the resultset. In this case there will be only
resultset open
at any time and the error doesn't come.

Thanks,
Nagarajan.


> -----Original Message-----
> From: Barry Lind [mailto:barry@xythos.com]
> Sent: Thursday, October 10, 2002 5:36 PM
> To: G.Nagarajan
> Cc: pgsql-jdbc@postgresql.org
> Subject: Re: [JDBC] NullPointer error returned from ResultSet.java
>
>
> Nagarajan,
>
> If you submit a complete test case that demonstrates the problem I would
> be willing to look into this.  But there just isn't enough information
> in what you have provided here to debug this.  I don't even know the
> exact error you are getting.
>
> thanks,
> --Barry
>
>
>
> G.Nagarajan wrote:
>  > Hi,
>  > I am also getting the same error. In my case I am using a prepared
> statement
>  > to create the resultset like
>  >
>  >   PreparedStatemet stmt = "...";
>  >
>  >   callerFunction()
>  >   {
>  >     createPreparedStatement();
>  >     fun1();
>  >     closePreparedStatement();
>  >   }
>  >   fun1()
>  >   {
>  >      Results rs = stmt.executeQuery();
>  >
>  >     while (rs.next() )
>  >     {
>  >         fun1(); //recursive call
>  >       }
>  >       rs.close();
>  >   }
>  >
>  > The null pointer exception is throws after three recursive calls. The
> code
>  > at
>  > 113 is rows.size(). Perhaps it has something to do with caching? The
> error
>  > doesn't come if I use normal statements.
>  >
>  > Regards,
>  > Nagarajan.
>  >
>  >
>  >
>  >>-----Original Message-----
>  >>From: pgsql-jdbc-owner@postgresql.org
>  >>[mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Jeff Duffy
>  >>Sent: Monday, October 07, 2002 5:57 PM
>  >>To: pgsql-jdbc@postgresql.org
>  >>Subject: [JDBC] NullPointer error returned from ResultSet.java
>  >>
>  >>
>  >>Greetings,
>  >>
>  >> I have a JSP page with a rather large scriptlet that uses multiple
> nested
>  >>statements and ResultSets. Under very specific conditions I am
>  >>encountering
>  >>the following error:
>  >>
>  >>java.lang.NullPointerException
>  >> at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
>  >>
>  >>
>  >>Here's one pertinent snippet of my code:
>  >>( This code begins inside a while(rs.next()) loop for ResultSet rs,
>  >>using Statement stmt)
>  >>
>  >>
>  >>sql.append("AN SQL QUERY");
>  >>rs2 = stmt2.executeQuery(sql.toString());
>  >>
>  >>while(rs2.next()){
>  >>     String courseId = rs2.getString(1);
>  >>     String roleName = rs2.getString(2);
>  >>
>  >>     rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
>  >>     rs3.next();
>  >>     assignmentId = rs3.getString(1);
>  >>
>  >>    sql.delete(0, sql.length());
>  >>    sql.append("AN SQL QUERY");
>  >>    stmt3.executeUpdate(sql.toString());
>  >>}
>  >>
>  >> Through debugging it appears that rs2 is throwing the error;
> the message
>  >>states the error is in method next(), but if I have only zero
> records or
>  >>only one record in rs2, there is no error. It's only when I
> have multiple
>  >>records that the error is thrown the second time round the while
>  >>loop, so I
>  >>suspect it may be thrown at the assignment of courseId.
>  >>
>  >> This error is reproduced given the same conditions (nested
>  >>ResultSets with
>  >>the innermost ResultSet executing an Update inside a
>  >>while(ResultSet.next())
>  >>loop.  It almost seems that the call to rs3.executeUpdate() is
>  >>closing rs2,
>  >>but I cannot see why that would happen. This is occurring in
> at least six
>  >>different source files.
>  >>
>  >> If I create a new Vector and stuff the contents of rs2 into it, then
>  >>iterate over the vector and use rs3 as shown above, no error is thrown.
>  >>
>  >> Updating to the 7.3beta1 JDBC driver changes the error message to:
>  >>
>  >>javax.servlet.ServletException: postgresql.con.closed
>  >>
>  >> which is even more puzzling.
>  >>
>  >>
>  >> Notes about my coding style that may be relevant:
>  >>- I use one StringBuffer for all sql queries and clear it for
>  >>each new query
>  >>for efficiency. This hasn't ever caused problems elsewhere.
>  >>
>  >>- I predeclare all Statement and ResultSet objects at the
>  >>outermost block of
>  >>code (the 'top') like so:
>  >>
>  >>Statement stmt   = conn.createStatement();
>  >>Statement stmt2  = conn.createStatement();
>  >>Statement stmt3  = conn.createStatement();
>  >>ResultSet rs, rs2, rs3;
>  >>
>  >>Other data:
>  >>
>  >>- The connections are obtained from a custom class that uses
> the jdbcpool
>  >>pool manager.
>  >>- I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
>  >>- The JDBC library is v7.2 (the pgjdbc2.jar binary download).
>  >>- PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
>  >>
>  >> Any flashes of insight?
>  >>
>  >>Thanks
>  >>
>  >>Jeff Duffy
>  >>jeff@alanne.com
>  >>
>  >>
>  >>---------------------------(end of
> broadcast)---------------------------
>  >>TIP 6: Have you searched our list archives?
>  >>
>  >>http://archives.postgresql.org
>  >
>  >
>  >
>  > ---------------------------(end of
> broadcast)---------------------------
>  > TIP 6: Have you searched our list archives?
>  >
>  > http://archives.postgresql.org
>  >
>
>


Re: NullPointer error returned from ResultSet.java

From
Barry Lind
Date:
Dave,

The driver does support having multiple result sets open at the same
time, and I do it frequently in my code.

Having two result sets open lets you nest queries as follows so that you
can use the results for one as input into the second:

PreparedStatement stmt1 = conn.prepareStatement("select bar1 from bar");
PreparedStatement stmt2 = conn.prepareStatement("select foo1 from foo "+
                                                 "where bar1 = ?");
ResultSet rset1 = stmt1.executeQuery();
while (rset1.next()) {
   //for each row in bar, update foo
   stmt2.setInt(1, rset1.getInt(1));
   ResultSet rset2 = stmt2.executeQuery();
   while (rset2.next()) {
     //do something useful with the results
   }
   rset2.close();
}
rset1.close();
stmt1.close();
stmt2.close();

thanks,
--Barry
Dave Cramer wrote:
> Jeff,
>
> You can't have two result sets open on the same connection.
>
> Dave
> On Mon, 2002-10-07 at 11:57, Jeff Duffy wrote:
>
>>Greetings,
>>
>> I have a JSP page with a rather large scriptlet that uses multiple nested
>>statements and ResultSets. Under very specific conditions I am encountering
>>the following error:
>>
>>java.lang.NullPointerException
>> at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
>>
>>
>>Here's one pertinent snippet of my code:
>>( This code begins inside a while(rs.next()) loop for ResultSet rs,
>>using Statement stmt)
>>
>>
>>sql.append("AN SQL QUERY");
>>rs2 = stmt2.executeQuery(sql.toString());
>>
>>while(rs2.next()){
>>     String courseId = rs2.getString(1);
>>     String roleName = rs2.getString(2);
>>
>>     rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
>>     rs3.next();
>>     assignmentId = rs3.getString(1);
>>
>>    sql.delete(0, sql.length());
>>    sql.append("AN SQL QUERY");
>>    stmt3.executeUpdate(sql.toString());
>>}
>>
>> Through debugging it appears that rs2 is throwing the error; the message
>>states the error is in method next(), but if I have only zero records or
>>only one record in rs2, there is no error. It's only when I have multiple
>>records that the error is thrown the second time round the while loop, so I
>>suspect it may be thrown at the assignment of courseId.
>>
>> This error is reproduced given the same conditions (nested ResultSets with
>>the innermost ResultSet executing an Update inside a while(ResultSet.next())
>>loop.  It almost seems that the call to rs3.executeUpdate() is closing rs2,
>>but I cannot see why that would happen. This is occurring in at least six
>>different source files.
>>
>> If I create a new Vector and stuff the contents of rs2 into it, then
>>iterate over the vector and use rs3 as shown above, no error is thrown.
>>
>> Updating to the 7.3beta1 JDBC driver changes the error message to:
>>
>>javax.servlet.ServletException: postgresql.con.closed
>>
>> which is even more puzzling.
>>
>>
>> Notes about my coding style that may be relevant:
>>- I use one StringBuffer for all sql queries and clear it for each new query
>>for efficiency. This hasn't ever caused problems elsewhere.
>>
>>- I predeclare all Statement and ResultSet objects at the outermost block of
>>code (the 'top') like so:
>>
>>Statement stmt   = conn.createStatement();
>>Statement stmt2  = conn.createStatement();
>>Statement stmt3  = conn.createStatement();
>>ResultSet rs, rs2, rs3;
>>
>>Other data:
>>
>>- The connections are obtained from a custom class that uses the jdbcpool
>>pool manager.
>>- I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
>>- The JDBC library is v7.2 (the pgjdbc2.jar binary download).
>>- PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
>>
>> Any flashes of insight?
>>
>>Thanks
>>
>>Jeff Duffy
>>jeff@alanne.com
>>
>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 6: Have you searched our list archives?
>>
>>http://archives.postgresql.org
>>
>>
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>




Re: NullPointer error returned from ResultSet.java

From
Dave Cramer
Date:
Barry, Jef,

I stand corrected!

Dave
On Thu, 2002-10-10 at 20:45, Barry Lind wrote:
> Dave,
>
> The driver does support having multiple result sets open at the same
> time, and I do it frequently in my code.
>
> Having two result sets open lets you nest queries as follows so that you
> can use the results for one as input into the second:
>
> PreparedStatement stmt1 = conn.prepareStatement("select bar1 from bar");
> PreparedStatement stmt2 = conn.prepareStatement("select foo1 from foo "+
>                                                  "where bar1 = ?");
> ResultSet rset1 = stmt1.executeQuery();
> while (rset1.next()) {
>    //for each row in bar, update foo
>    stmt2.setInt(1, rset1.getInt(1));
>    ResultSet rset2 = stmt2.executeQuery();
>    while (rset2.next()) {
>      //do something useful with the results
>    }
>    rset2.close();
> }
> rset1.close();
> stmt1.close();
> stmt2.close();
>
> thanks,
> --Barry
> Dave Cramer wrote:
> > Jeff,
> >
> > You can't have two result sets open on the same connection.
> >
> > Dave
> > On Mon, 2002-10-07 at 11:57, Jeff Duffy wrote:
> >
> >>Greetings,
> >>
> >> I have a JSP page with a rather large scriptlet that uses multiple nested
> >>statements and ResultSets. Under very specific conditions I am encountering
> >>the following error:
> >>
> >>java.lang.NullPointerException
> >> at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
> >>
> >>
> >>Here's one pertinent snippet of my code:
> >>( This code begins inside a while(rs.next()) loop for ResultSet rs,
> >>using Statement stmt)
> >>
> >>
> >>sql.append("AN SQL QUERY");
> >>rs2 = stmt2.executeQuery(sql.toString());
> >>
> >>while(rs2.next()){
> >>     String courseId = rs2.getString(1);
> >>     String roleName = rs2.getString(2);
> >>
> >>     rs3 = stmt3.executeQuery("SELECT nextval('assignment_id')");
> >>     rs3.next();
> >>     assignmentId = rs3.getString(1);
> >>
> >>    sql.delete(0, sql.length());
> >>    sql.append("AN SQL QUERY");
> >>    stmt3.executeUpdate(sql.toString());
> >>}
> >>
> >> Through debugging it appears that rs2 is throwing the error; the message
> >>states the error is in method next(), but if I have only zero records or
> >>only one record in rs2, there is no error. It's only when I have multiple
> >>records that the error is thrown the second time round the while loop, so I
> >>suspect it may be thrown at the assignment of courseId.
> >>
> >> This error is reproduced given the same conditions (nested ResultSets with
> >>the innermost ResultSet executing an Update inside a while(ResultSet.next())
> >>loop.  It almost seems that the call to rs3.executeUpdate() is closing rs2,
> >>but I cannot see why that would happen. This is occurring in at least six
> >>different source files.
> >>
> >> If I create a new Vector and stuff the contents of rs2 into it, then
> >>iterate over the vector and use rs3 as shown above, no error is thrown.
> >>
> >> Updating to the 7.3beta1 JDBC driver changes the error message to:
> >>
> >>javax.servlet.ServletException: postgresql.con.closed
> >>
> >> which is even more puzzling.
> >>
> >>
> >> Notes about my coding style that may be relevant:
> >>- I use one StringBuffer for all sql queries and clear it for each new query
> >>for efficiency. This hasn't ever caused problems elsewhere.
> >>
> >>- I predeclare all Statement and ResultSet objects at the outermost block of
> >>code (the 'top') like so:
> >>
> >>Statement stmt   = conn.createStatement();
> >>Statement stmt2  = conn.createStatement();
> >>Statement stmt3  = conn.createStatement();
> >>ResultSet rs, rs2, rs3;
> >>
> >>Other data:
> >>
> >>- The connections are obtained from a custom class that uses the jdbcpool
> >>pool manager.
> >>- I'm using the Sun JDK v1.4.0 for Linux (it happens on Win32 as well).
> >>- The JDBC library is v7.2 (the pgjdbc2.jar binary download).
> >>- PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
> >>
> >> Any flashes of insight?
> >>
> >>Thanks
> >>
> >>Jeff Duffy
> >>jeff@alanne.com
> >>
> >>
> >>---------------------------(end of broadcast)---------------------------
> >>TIP 6: Have you searched our list archives?
> >>
> >>http://archives.postgresql.org
> >>
> >>
> >
> >
> >
> >
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> >
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>



Re: NullPointer error returned from ResultSet.java

From
Jeffrey Duffy
Date:
On Friday, October 11, 2002, at 05:05 AM, Dave Cramer wrote:

> Barry, Jef,
>
> I stand corrected!
>
> Dave
> On Thu, 2002-10-10 at 20:45, Barry Lind wrote:
>> Dave,
>>
>> The driver does support having multiple result sets open at the same
>> time, and I do it frequently in my code.

  Then I guess I still have the question of where this error is coming
from.  I grepped for 'postgresql.con.closed' in the source thinking it
may be a PSQLException error message, but didn't see it in there.  I'll
poke about in jdbc2/Statement.java and so forth to see if I can think
of a reason that this particular situation would occur.  If you have
any thoughts on where to look, I'd be grateful.

  Jeff Duffy
jeff@alanne.com


Re: NullPointer error returned from ResultSet.java

From
Dave Cramer
Date:
Jeff,

Can you show me how the statements are being created? I was partially
correct, you can't have multiple result sets open on the same statement.

Dave
On Fri, 2002-10-11 at 06:43, Jeffrey Duffy wrote:
>
> On Friday, October 11, 2002, at 05:05 AM, Dave Cramer wrote:
>
> > Barry, Jef,
> >
> > I stand corrected!
> >
> > Dave
> > On Thu, 2002-10-10 at 20:45, Barry Lind wrote:
> >> Dave,
> >>
> >> The driver does support having multiple result sets open at the same
> >> time, and I do it frequently in my code.
>
>   Then I guess I still have the question of where this error is coming
> from.  I grepped for 'postgresql.con.closed' in the source thinking it
> may be a PSQLException error message, but didn't see it in there.  I'll
> poke about in jdbc2/Statement.java and so forth to see if I can think
> of a reason that this particular situation would occur.  If you have
> any thoughts on where to look, I'd be grateful.
>
>   Jeff Duffy
> jeff@alanne.com
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
>



Re: NullPointer error returned from ResultSet.java

From
Jeffrey Duffy
Date:
On Friday, October 11, 2002, at 06:14 AM, Dave Cramer wrote:

> Jeff,
>
> Can you show me how the statements are being created? I was partially
> correct, you can't have multiple result sets open on the same
> statement.
>
Dave,

  Before I enter the first block, I predeclare all Statements and
ResultSets that will be used like so:

Statement stmt   = conn.createStatement();
Statement stmt2  = conn.createStatement();
Statement stmt3  = conn.createStatement();
ResultSet rs, rs2, rs3;

  I know you can't have many RS's per statement, but I'm very sure that
I am not attempting to do that. As I said before, this only seems to
happen when I have an executeUpdate call from within a nested rs.next()
loop, but it's definitely not the same statement object, which is why
it's puzzling.

Jeff


Re: NullPointer error returned from ResultSet.java

From
Dave Cramer
Date:
Jeff,

Can you supply me with a test case, and I can debug the driver and see
what is going on?

Dave
On Fri, 2002-10-11 at 08:35, Jeffrey Duffy wrote:
>
> On Friday, October 11, 2002, at 06:14 AM, Dave Cramer wrote:
>
> > Jeff,
> >
> > Can you show me how the statements are being created? I was partially
> > correct, you can't have multiple result sets open on the same
> > statement.
> >
> Dave,
>
>   Before I enter the first block, I predeclare all Statements and
> ResultSets that will be used like so:
>
> Statement stmt   = conn.createStatement();
> Statement stmt2  = conn.createStatement();
> Statement stmt3  = conn.createStatement();
> ResultSet rs, rs2, rs3;
>
>   I know you can't have many RS's per statement, but I'm very sure that
> I am not attempting to do that. As I said before, this only seems to
> happen when I have an executeUpdate call from within a nested rs.next()
> loop, but it's definitely not the same statement object, which is why
> it's puzzling.
>
> Jeff
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
>



Re: NullPointer error returned from ResultSet.java

From
Jeffrey Duffy
Date:
On Friday, October 11, 2002, at 08:21 AM, Dave Cramer wrote:

> Jeff,
>
> Can you supply me with a test case, and I can debug the driver and see
> what is going on?

  I must sheepishly admit that I wrote a pure Java test case and it
didn't break. Then I wrote a JSP test case using the classloader, and
it did not break. Then, finally, I wrote a JSP test case using the
jdbcpool connection pool manager, and bam, it broke. I don't know what
weird interaction the pool manager has with the driver internals (the
exception is being thrown in resultset.next, but who knows), so I guess
I'll have to look deeper into this before I  come back to the driver as
the culprit.

Jeff Duffy