RE: NullPointerException when using JDBC ResultSet next() method
Maybe reading the code wrong but you're declaring the variable rs on the third line of doGet(...) but putting the return value of the stmt.executeQuery() in a variable called rset. Then you call next() on the original variable, which is null ;) A few other points: You are definately loading the driver otherwise a ClassNotFoundException would be thrown from Class.forName(...). You also would not have been able to get a Connection or prepare a Statement object. Mixing application logic and presentation code in a servlet can lead to a maintenance nightmare. You can use custom jsp iterator tags to separate out the application code. We use these a lot ;) Also, from a maintenance point of view you may want to encapsulate the database connection/sql generation code into domain specific classes. The connection parameters and, possibly, the sql could then be placed in configuration files. Makes changing database easy ;) Hope this helps, Jon -Original Message- From: Philip Kazmier, CEM RD [mailto:[EMAIL PROTECTED]] Sent: 08 April 2002 22:55 To: 'Tomcat Users List' Subject: RE: NullPointerException when using JDBC ResultSet next() method I apologize but I posted the wrong code. Here is the correct code. Line 45 is rs.next. I am not sure that the driver has loaded properly. How can I tell? Thanks. import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ListAllOpenBugs extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Connection con = null; //Statement stmt = null; ResultSet rs = null; int i = 0; res.setContentType(text/html); PrintWriter out = res.getWriter(); try { // Load Oracle driver //DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // Load the MySQL driver Class.forName(org.gjt.mm.mysql.Driver); // Connect to the local database //Connection conn = //DriverManager.getConnection(jdbc:oracle:thin:172.24.230.20:1521:ORCL, system, manager); // Get a connection to the database con = DriverManager.getConnection(jdbc:mysql://localhost:3306/pssoftware?user=phi lkpassword=kirov); // Query the employee names Statement stmt = con.createStatement (); //ResultSet rset = stmt.executeQuery (select b.bug_row_id, b.status, ps.name, b.short_desc, b.date_opened from bug b, ps_software ps where b.software_id = ps.pss_row_id and b.status = 1); ResultSet rset = stmt.executeQuery (select sc_id from bug_status_codes); // Display the result set as a list out.println(HTMLHEADTITLEAll Open Bugs/TITLE); out.println(LINK REL=STYLESHEET HREF=\..\\NICEStyle.css\); out.println(/HEAD); out.println(BODY); out.println(TABLE); while(rs.next()) { // if (i == 1) // { // i=0; //out.println(TRTD + rs.getString(bug_row_id) + /TDTD + rs.getString(name) + /TDTD + rs.getString(status) + /TDTD + rs.getString(date_opened) + /TDTD + rs.getString(short_desc) + /TD/TR); //out.println(TRTD + rs.getString(bug_row_id) + /TD/TR); out.println(TRTD + rs.getString(sc_id) + /TD/TR); // } // else // { // i=1; //out.println(TRTD + rs.getString(bug_row_id) + /TDTD + rs.getString(name) + /TDTD + rs.getString(status) + /TDTD + rs.getString(date_opened) + /TDTD + rs.getString(short_desc) + /TD/TR); //out.println(TRTD + rs.getString(bug_row_id) + /TD/TR); // } } out.println(/TABLE); out.println(/BODY/HTML); } catch (ClassNotFoundException e) { out.println(Couldn't load the database driver- + e.getMessage()); } catch(SQLException e) { out.println(SQLException caught: + e.getMessage()); } finally { try { if (con != null
RE: NullPointerException when using JDBC ResultSet next() method
rs is NOT Being set -- you are getting the results into rset so use rset.next() etc. Best Wishes John Burgess [EMAIL PROTECTED] Tel: 01865 718666 Fax: 01865 718600 -Original Message- From: Philip Kazmier, CEM RD [mailto:[EMAIL PROTECTED]] Sent: Monday, April 08, 2002 10:55 PM To: 'Tomcat Users List' Subject: RE: NullPointerException when using JDBC ResultSet next() method I apologize but I posted the wrong code. Here is the correct code. Line 45 is rs.next. I am not sure that the driver has loaded properly. How can I tell? Thanks. import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ListAllOpenBugs extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Connection con = null; //Statement stmt = null; ResultSet rs = null; int i = 0; res.setContentType(text/html); PrintWriter out = res.getWriter(); try { // Load Oracle driver //DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // Load the MySQL driver Class.forName(org.gjt.mm.mysql.Driver); // Connect to the local database //Connection conn = //DriverManager.getConnection(jdbc:oracle:thin:172.24.230.20:1521:ORCL, system, manager); // Get a connection to the database con = DriverManager.getConnection(jdbc:mysql://localhost:3306/pssoftware?user=phi lkpassword=kirov); // Query the employee names Statement stmt = con.createStatement (); //ResultSet rset = stmt.executeQuery (select b.bug_row_id, b.status, ps.name, b.short_desc, b.date_opened from bug b, ps_software ps where b.software_id = ps.pss_row_id and b.status = 1); ResultSet rset = stmt.executeQuery (select sc_id from bug_status_codes); // Display the result set as a list out.println(HTMLHEADTITLEAll Open Bugs/TITLE); out.println(LINK REL=STYLESHEET HREF=\..\\NICEStyle.css\); out.println(/HEAD); out.println(BODY); out.println(TABLE); while(rs.next()) { // if (i == 1) // { // i=0; //out.println(TRTD + rs.getString(bug_row_id) + /TDTD + rs.getString(name) + /TDTD + rs.getString(status) + /TDTD + rs.getString(date_opened) + /TDTD + rs.getString(short_desc) + /TD/TR); //out.println(TRTD + rs.getString(bug_row_id) + /TD/TR); out.println(TRTD + rs.getString(sc_id) + /TD/TR); // } // else // { // i=1; //out.println(TRTD + rs.getString(bug_row_id) + /TDTD + rs.getString(name) + /TDTD + rs.getString(status) + /TDTD + rs.getString(date_opened) + /TDTD + rs.getString(short_desc) + /TD/TR); //out.println(TRTD + rs.getString(bug_row_id) + /TD/TR); // } } out.println(/TABLE); out.println(/BODY/HTML); } catch (ClassNotFoundException e) { out.println(Couldn't load the database driver- + e.getMessage()); } catch(SQLException e) { out.println(SQLException caught: + e.getMessage()); } finally { try { if (con != null) con.close(); } catch(SQLException ignored) { } } } }; -Original Message- From: August Detlefsen [mailto:[EMAIL PROTECTED]] Sent: Monday, April 08, 2002 1:19 PM To: Tomcat Users List Subject: Re: NullPointerException when using JDBC ResultSet next() method The stack trace says that your NullPointer occurs on line 45, but line 45 is blank. Is there some more code that you didn't ost? Import statements, perhaps? I think it probably happened here: stmt = con.createStatement(); -IE: You were not able to get a Connection, con is null and calling its methods will give you the NullPointer. Are you sure the driver loaded properly
Re: NullPointerException when using JDBC ResultSet next() method
sorry, but you called ResultSet rset = stmt.executeQuery (select sc_id from bug_status_codes); and then used rs.next() maybe they must be all rs or all rset. Bye Giorgio -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
RE: NullPointerException when using JDBC ResultSet next() method
Thank you Giorgio! That was it! Boy do I feel stupid. Thanks again. -Original Message- From: Giorgio Ponza [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 2:15 AM To: Tomcat Users List Subject: Re: NullPointerException when using JDBC ResultSet next() method sorry, but you called ResultSet rset = stmt.executeQuery (select sc_id from bug_status_codes); and then used rs.next() maybe they must be all rs or all rset. Bye Giorgio -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED] -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
RE: NullPointerException when using JDBC ResultSet next() method
Thanks. That solved it. -Original Message- From: Ralph Einfeldt [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 12:18 AM To: Tomcat Users List Subject: AW: NullPointerException when using JDBC ResultSet next() method Your have 2 ResultSets: rs and rset. You assign to the rset but call rs.next(). So rs is null. -Ursprüngliche Nachricht- Von: Philip Kazmier, CEM RD [mailto:[EMAIL PROTECTED]] Gesendet: Montag, 8. April 2002 23:55 An: 'Tomcat Users List' Betreff: RE: NullPointerException when using JDBC ResultSet next() method ResultSet rs = null; ResultSet rset = stmt.executeQuery (select sc_id from bug_status_codes); while(rs.next()) -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED] -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
Re: NullPointerException when using JDBC ResultSet next() method
You sure that your test table has a test column? Change rs.getString (test) to rs.getString(1) to see if that works... HTH Philip Kazmier, CEM To: '[EMAIL PROTECTED]' RD [EMAIL PROTECTED] philip.kazmiercc: @nice.com Subject: NullPointerException when using JDBC ResultSet next() method 04/08/02 12:55 PM Please respond to Tomcat Users List I am getting this error in a servlet compiled on Win2K, using JDK1.3.1 and Tomcat 3.3 with MySQL 3.23.47: Location: /PSSoftware/servlet/ListAllOpenBugs Internal Servlet Error: java.lang.NullPointerException at ListAllOpenBugs.doGet(ListAllOpenBugs.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java) at javax.servlet.http.HttpServlet.service(HttpServlet.java) at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source) at org.apache.tomcat.core.Handler.invoke(Unknown Source) at org.apache.tomcat.core.Handler.service(Unknown Source) at org.apache.tomcat.facade.ServletHandler.service(Unknown Source) at org.apache.tomcat.core.ContextManager.internalService(Unknown Source) at org.apache.tomcat.core.ContextManager.service(Unknown Source) at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source) at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source) at java.lang.Thread.run(Thread.java:484) Here is the servlet code: public class MySQLTest extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Connection con = null; Statement stmt = null; ResultSet rs = null; res.setContentType(text/html); PrintWriter out = res.getWriter(); try { // Load the MySQL driver Class.forName(org.gjt.mm.mysql.Driver); //try { //Class.forName(twz1.jdbc.mysql.jdbcMysqlDriver); //} //catch(Exception e){out.println(e);} // Get a connection to the database con = DriverManager.getConnection(jdbc:mysql://localhost:3306/test?user=rootpass word=typhoon); // Create a statement object stmt = con.createStatement(); // Execute and SQL query, get a result set rs = stmt.executeQuery(SELECT * from test); // Display the result set as a list out.println(HTMLHEADTITLETest/TITLE/HEAD); out.println(BODY); out.println(UL); while(rs.next()) { out.println(LI + rs.getString (test)); } out.println(/UL); out.println(/BODY/HTML); } catch (ClassNotFoundException e) { out.println(Couldn't load the database driver- + e.getMessage()); }
Re: NullPointerException when using JDBC ResultSet next() method
The stack trace says that your NullPointer occurs on line 45, but line 45 is blank. Is there some more code that you didn't ost? Import statements, perhaps? I think it probably happened here: stmt = con.createStatement(); -IE: You were not able to get a Connection, con is null and calling its methods will give you the NullPointer. Are you sure the driver loaded properly? --- Philip Kazmier, CEM RD [EMAIL PROTECTED] wrote: I am getting this error in a servlet compiled on Win2K, using JDK1.3.1 and Tomcat 3.3 with MySQL 3.23.47: Location: /PSSoftware/servlet/ListAllOpenBugs Internal Servlet Error: java.lang.NullPointerException at ListAllOpenBugs.doGet(ListAllOpenBugs.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java) at javax.servlet.http.HttpServlet.service(HttpServlet.java) at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source) at org.apache.tomcat.core.Handler.invoke(Unknown Source) at org.apache.tomcat.core.Handler.service(Unknown Source) at org.apache.tomcat.facade.ServletHandler.service(Unknown Source) at org.apache.tomcat.core.ContextManager.internalService(Unknown Source) at org.apache.tomcat.core.ContextManager.service(Unknown Source) at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source) at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source) at java.lang.Thread.run(Thread.java:484) Here is the servlet code: public class MySQLTest extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Connection con = null; Statement stmt = null; ResultSet rs = null; res.setContentType(text/html); PrintWriter out = res.getWriter(); try { // Load the MySQL driver Class.forName(org.gjt.mm.mysql.Driver); //try { //Class.forName(twz1.jdbc.mysql.jdbcMysqlDriver); //} //catch(Exception e){out.println(e);} // Get a connection to the database con = DriverManager.getConnection(jdbc:mysql://localhost:3306/test?user=rootpass word=typhoon); // Create a statement object stmt = con.createStatement(); // Execute and SQL query, get a result set rs = stmt.executeQuery(SELECT * from test); // Display the result set as a list out.println(HTMLHEADTITLETest/TITLE/HEAD); out.println(BODY); out.println(UL); while(rs.next()) { out.println(LI + rs.getString(test)); } out.println(/UL); out.println(/BODY/HTML); } catch (ClassNotFoundException e) { out.println(Couldn't load the database driver- + e.getMessage()); } catch(SQLException e) { out.println(SQLException caught: + e.getMessage()); } finally { try { if (con != null) con.close(); } catch(SQLException ignored) { } } } }; As you can see I tried this with Oracle. I get the same result when I used Oracle. Thanks. Phil Kazmier Developer, CEM NICE Systems Denver, Colorado Office (720) 264-4284 -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED] __ Do You Yahoo!? Yahoo! Tax Center - online filing with TurboTax http://taxes.yahoo.com/ -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
RE: NullPointerException when using JDBC ResultSet next() method
I apologize but I posted the wrong code. Here is the correct code. Line 45 is rs.next. I am not sure that the driver has loaded properly. How can I tell? Thanks. import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ListAllOpenBugs extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Connection con = null; //Statement stmt = null; ResultSet rs = null; int i = 0; res.setContentType(text/html); PrintWriter out = res.getWriter(); try { // Load Oracle driver //DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // Load the MySQL driver Class.forName(org.gjt.mm.mysql.Driver); // Connect to the local database //Connection conn = //DriverManager.getConnection(jdbc:oracle:thin:172.24.230.20:1521:ORCL, system, manager); // Get a connection to the database con = DriverManager.getConnection(jdbc:mysql://localhost:3306/pssoftware?user=phi lkpassword=kirov); // Query the employee names Statement stmt = con.createStatement (); //ResultSet rset = stmt.executeQuery (select b.bug_row_id, b.status, ps.name, b.short_desc, b.date_opened from bug b, ps_software ps where b.software_id = ps.pss_row_id and b.status = 1); ResultSet rset = stmt.executeQuery (select sc_id from bug_status_codes); // Display the result set as a list out.println(HTMLHEADTITLEAll Open Bugs/TITLE); out.println(LINK REL=STYLESHEET HREF=\..\\NICEStyle.css\); out.println(/HEAD); out.println(BODY); out.println(TABLE); while(rs.next()) { // if (i == 1) // { // i=0; //out.println(TRTD + rs.getString(bug_row_id) + /TDTD + rs.getString(name) + /TDTD + rs.getString(status) + /TDTD + rs.getString(date_opened) + /TDTD + rs.getString(short_desc) + /TD/TR); //out.println(TRTD + rs.getString(bug_row_id) + /TD/TR); out.println(TRTD + rs.getString(sc_id) + /TD/TR); // } // else // { // i=1; //out.println(TRTD + rs.getString(bug_row_id) + /TDTD + rs.getString(name) + /TDTD + rs.getString(status) + /TDTD + rs.getString(date_opened) + /TDTD + rs.getString(short_desc) + /TD/TR); //out.println(TRTD + rs.getString(bug_row_id) + /TD/TR); // } } out.println(/TABLE); out.println(/BODY/HTML); } catch (ClassNotFoundException e) { out.println(Couldn't load the database driver- + e.getMessage()); } catch(SQLException e) { out.println(SQLException caught: + e.getMessage()); } finally { try { if (con != null) con.close(); } catch(SQLException ignored) { } } } }; -Original Message- From: August Detlefsen [mailto:[EMAIL PROTECTED]] Sent: Monday, April 08, 2002 1:19 PM To: Tomcat Users List Subject: Re: NullPointerException when using JDBC ResultSet next() method The stack trace says that your NullPointer occurs on line 45, but line 45 is blank. Is there some more code that you didn't ost? Import statements, perhaps? I think it probably happened here: stmt = con.createStatement(); -IE: You were not able to get a Connection, con is null and calling its methods will give you the NullPointer. Are you sure the driver loaded properly? --- Philip Kazmier, CEM RD [EMAIL PROTECTED] wrote: I am getting this error in a servlet compiled on Win2K, using JDK1.3.1 and Tomcat 3.3 with MySQL 3.23.47: Location: /PSSoftware/servlet/ListAllOpenBugs Internal Servlet Error: java.lang.NullPointerException at ListAllOpenBugs.doGet(ListAllOpenBugs.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java