Thread: java.lang.NullPointerException on imbricated queries
<font face="Courier New, Courier">Hello,<br /> <br /> We </font>have a web application in Apache, Posgresql 7.2, servletjava (JDK 1.2), Jserv and jdbc7.2dev-1.2.jar. <br /> A lot of programs runs normally with JDBC access to postgresql.But when we use two imbricated queries like this example, a message <font face="Courier New, Courier">JAVALANGEXEPTIONjava.lang.NullPointerException occurs</font>:<br /><br /><font face="Courier New, Courier">1 Query1= </font><font color="#00CB00" face="Courier New, Courier">"SELECT * FROM example1;"</font><font face="CourierNew, Courier">;<br /> 2 rs1 = stmt.executeQuery(Query1);<br /> 3 </font><font color="#941EDF" face="CourierNew, Courier">while</font><font face="Courier New, Courier"> (rs1.next()) {<br /> 4 Query2 = </font><fontcolor="#00CB00" face="Courier New, Courier">"SELECT * FROM example2;<br /></font><font face="Courier New, Courier">5 rs2= stmt.executeQuery(Query2);<br /> 6 </font><font color="#941EDF" face="CourierNew, Courier">while</font><font face="Courier New, Courier"> (rs2.next()) {<br /> 7 //Something .../...<br /> 8 }<br /> 9 } <br /><br/> Programme DetailsRapports.java: JAVALANGEXEPTION java.lang.NullPointerException: <br /> java.lang.NullPointerException:<br /> at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)<br /> atDetailsRapports.AfficheEntrees(DetailsRapports.java:68)<br /> at DetailsRapports.service(DetailsRapports.java:43)<br/> at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)<br/> at org.apache.jserv.JServConnection.processRequest(JServConnection.java:317)<br/> at org.apache.jserv.JServConnection.run(JServConnection.java:188)<br/> at java.lang.Thread.run(Thread.java:479)<br /><br/> This message occurs on line 3 "</font><font color="#941EDF" face="Courier New, Courier">while</font><font face="CourierNew, Courier"> (rs1.next()) {" after a first good Query1 and a first good Query2.<br /><br /> Any help wouldbe appreciated.<br /><br /><br /></font><p> --------------------------------------------------------------------------<br/> Olivier BULLAT<br /> Responsable du Centrede Ressources Informatiques<br /> Institut Francais de Mecanique Avancee - IFMA<br /> Campus de CLERMONT-FERRAND /Les Cezeaux<br /> BP 265<br /> 63175 AUBIERE Cedex<br /> Tel: 04.73.28.80.35<br /> Fax: 04.73.28.81.00<br /> mail: <fontcolor="#0000FF"><u>Olivier.Bullat@ifma.fr<br /><a eudora="autourl" href="http://www.ifma.fr/">http://www.ifma.fr</a><br /></u></font>--------------------------------------------------------------------------
Are you using GNU's jdk or Sun's jdk?
Regards,
Kallol.
-----Original Message-----Hello,
From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Olivier BULLAT
Sent: Monday, June 30, 2003 2:29 PM
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] java.lang.NullPointerException on imbricated queries
We have a web application in Apache, Posgresql 7.2, servlet java (JDK 1.2), Jserv and jdbc7.2dev-1.2.jar.
A lot of programs runs normally with JDBC access to postgresql. But when we use two imbricated queries like this example, a message JAVALANGEXEPTION java.lang.NullPointerException occurs:
1 Query1 = "SELECT * FROM example1;";
2 rs1 = stmt.executeQuery(Query1);
3 while (rs1.next()) {
4 Query2 = "SELECT * FROM example2;
5 rs2 = stmt.executeQuery(Query2);
6 while (rs2.next()) {
7 // Something .../...
8 }
9 }
Programme DetailsRapports.java: JAVALANGEXEPTION java.lang.NullPointerException:
java.lang.NullPointerException:
at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
at DetailsRapports.AfficheEntrees(DetailsRapports.java:68)
at DetailsRapports.service(DetailsRapports.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)
at org.apache.jserv.JServConnection.processRequest(JServConnection.java:317)
at org.apache.jserv.JServConnection.run(JServConnection.java:188)
at java.lang.Thread.run(Thread.java:479)
This message occurs on line 3 "while (rs1.next()) {" after a first good Query1 and a first good Query2.
Any help would be appreciated.--------------------------------------------------------------------------
Olivier BULLAT
Responsable du Centre de Ressources Informatiques
Institut Francais de Mecanique Avancee - IFMA
Campus de CLERMONT-FERRAND / Les Cezeaux
BP 265
63175 AUBIERE Cedex
Tel: 04.73.28.80.35
Fax: 04.73.28.81.00
mail: Olivier.Bullat@ifma.fr
http://www.ifma.fr
--------------------------------------------------------------------------
Each Statement may only have 1 ResultSet open at a time. If you want to access multiple Results at the same time, you need to use 2 different Statements. See below 1 Query1 = "SELECT * FROM example1;"; 2 rs1 = stmt1.executeQuery(Query1); 3 while (rs1.next()) { 4 Query2 = "SELECT * FROM example2; 5 rs2 = stmt2.executeQuery(Query2); 6 while (rs2.next()) { 7 // Something .../... 8 } 9 } Garrick On Mon, 2003-06-30 at 04:06, Kallol Nandi wrote: > Are you using GNU's jdk or Sun's jdk? > > Regards, > Kallol. > -----Original Message----- > From: pgsql-jdbc-owner@postgresql.org > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Olivier BULLAT > Sent: Monday, June 30, 2003 2:29 PM > To: pgsql-jdbc@postgresql.org > Subject: [JDBC] java.lang.NullPointerException on imbricated queries > > > Hello, > > We have a web application in Apache, Posgresql 7.2, servlet java (JDK > 1.2), Jserv and jdbc7.2dev-1.2.jar. > A lot of programs runs normally with JDBC access to postgresql. But when > we use two imbricated queries like this example, a message JAVALANGEXEPTION > java.lang.NullPointerException occurs: > > 1 Query1 = "SELECT * FROM example1;"; > 2 rs1 = stmt.executeQuery(Query1); > 3 while (rs1.next()) { > 4 Query2 = "SELECT * FROM example2; > 5 rs2 = stmt.executeQuery(Query2); > 6 while (rs2.next()) { > 7 // Something .../... > 8 } > 9 } > > Programme DetailsRapports.java: JAVALANGEXEPTION > java.lang.NullPointerException: > java.lang.NullPointerException: > at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113) > at DetailsRapports.AfficheEntrees(DetailsRapports.java:68) > at DetailsRapports.service(DetailsRapports.java:43) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:588) > at > org.apache.jserv.JServConnection.processRequest(JServConnection.java:317) > at org.apache.jserv.JServConnection.run(JServConnection.java:188) > at java.lang.Thread.run(Thread.java:479) > > This message occurs on line 3 "while (rs1.next()) {" after a first good > Query1 and a first good Query2. > > Any help would be appreciated. > > > > -------------------------------------------------------------------------- > Olivier BULLAT > Responsable du Centre de Ressources Informatiques > Institut Francais de Mecanique Avancee - IFMA > Campus de CLERMONT-FERRAND / Les Cezeaux > BP 265 > 63175 AUBIERE Cedex > Tel: 04.73.28.80.35 > Fax: 04.73.28.81.00 > mail: Olivier.Bullat@ifma.fr > http://www.ifma.fr > -------------------------------------------------------------------------- >
Olivier, One statement can, by default, have only one result set, when you execute the second query the first result set is closed. You'll need to rewrite your code to something like: Statement innerStmt = conn.createStatement(); Query1 = "SELECT * FROM example1;"; rs1 = stmt.executeQuery(Query1); while (rs1.next()) { Query2 = "SELECT * FROM example2; rs2 = innerStmt.executeQuery(Query2); while (rs2.next()) { // Something .../... } } The postgresql jdbc driver probably shouldn't npe in these cases, Hope this helps, Michael Olivier BULLAT wrote: > Hello, > > We have a web application in Apache, Posgresql 7.2, servlet java (JDK > 1.2), Jserv and jdbc7.2dev-1.2.jar. > A lot of programs runs normally with JDBC access to postgresql. But when > we use two imbricated queries like this example, a message > JAVALANGEXEPTION java.lang.NullPointerException occurs: > > 1 Query1 = "SELECT * FROM example1;"; > 2 rs1 = stmt.executeQuery(Query1); > 3 while (rs1.next()) { > 4 Query2 = "SELECT * FROM example2; > 5 rs2 = stmt.executeQuery(Query2); > 6 while (rs2.next()) { > 7 // Something .../... > 8 } > 9 } > > Programme DetailsRapports.java: JAVALANGEXEPTION > java.lang.NullPointerException: > java.lang.NullPointerException: > at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113) > at DetailsRapports.AfficheEntrees(DetailsRapports.java:68) > at DetailsRapports.service(DetailsRapports.java:43) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:588) > at > org.apache.jserv.JServConnection.processRequest(JServConnection.java:317) > at org.apache.jserv.JServConnection.run(JServConnection.java:188) > at java.lang.Thread.run(Thread.java:479) > > This message occurs on line 3 "while (rs1.next()) {" after a first good > Query1 and a first good Query2. > > Any help would be appreciated. > > > -------------------------------------------------------------------------- > Olivier BULLAT > Responsable du Centre de Ressources Informatiques > Institut Francais de Mecanique Avancee - IFMA > Campus de CLERMONT-FERRAND / Les Cezeaux > BP 265 > 63175 AUBIERE Cedex > Tel: 04.73.28.80.35 > Fax: 04.73.28.81.00 > mail: _Olivier.Bullat@ifma.fr > http://www.ifma.fr <http://www.ifma.fr/> > _-------------------------------------------------------------------------- > -- Web Applications Developer Open World Ltd, 11 Riverside Court, Riverside Road, Bath, BA2 3DZ. Tel: +44 1225 444950 Fax: +44 1225 336738 http://www.openworld.co.uk/ CONFIDENTIALITY NOTICE The information contained in this message is confidential, intended only for the use of the individual or the entity named as recipient. If the reader of this message is not that recipient, you are notified that any dissemination, distribution or copy of this message is strictly prohibited. If you have received this message in error, please immediately notify us by telephone on the number above. Your co-operation is appreciated.