8 Replies Latest reply: Sep 18, 2006 5:54 PM by jschellSomeoneStoleMyAlias RSS

    Null Pointer when Closing JDBC Connection

    807569
      I'm getting a null pointer error when trying to close my statement, connection, and result sets. My progam leaves the SQL query class I made to work with the resultset in another class, then returns to the SQL query class to close the connections. I am able to work with the result set in the other class OK, but I cannot close the connections for some reason. Do I need to do something special to close these items since I am leaving and returning to the class that creates them? Thanks for the help!
        • 1. Re: Null Pointer when Closing JDBC Connection
          807569
          The variable which is supposed to hold the reference to the Connection is being lost somewhere (null assignment). Where is the relevant code?
          • 2. Re: Null Pointer when Closing JDBC Connection
            807569
            I think this is all of the relevant code.

            Class 1 uses the JDBC here:
            public static ResultSet rs;
            private static Statement s;
            private static Connection con;
            
            private void databaseQuery()
                 {
            
                      try 
                      {
                              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                              String dataSourceName = "Mail_List";
                              String dbURL = "jdbc:odbc:" + dataSourceName;
                              Connection con = DriverManager.getConnection(dbURL,"",""); 
                              Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                           
                           if(criteriaBox.getSelectedIndex() == 0) searchCriteria = "class";
                           if(criteriaBox.getSelectedIndex() == 1) searchCriteria = "lastName";
                           if(criteriaBox.getSelectedIndex() == 2) searchCriteria = "company";
                           if(criteriaBox.getSelectedIndex() == 3) searchCriteria = "city";
                           if(criteriaBox.getSelectedIndex() == 4) searchCriteria = "county";
                           if(criteriaBox.getSelectedIndex() == 5) searchCriteria = "region";
            
                           System.out.println("select * from Contacts where "+ searchCriteria +" = '" + searchField.getText()+"'"); 
                              s.execute("select * from Contacts where "+ searchCriteria +" = '" + searchField.getText()+"'"); 
            
                              rs = s.getResultSet();
                           hasResults = false;
                           recordCount = 1;
                           rowCount = 0;
            
                           if(rs != null)
                           {
                                rs.last();
                                totalRows = rs.getRow();
                                rs.beforeFirst();
                                System.out.println("Total Rows: "+totalRows);
                           }
                                                       
                           if (rs != null) while ( rs.next() ) 
                           {
                                hasResults = true;
                      
                                     for(columnCount = 0; columnCount < 9; columnCount++)
                                     {
                                          //System.out.println(rs.getString(recordCount));
                                          SearchMenu.fieldArray[rowCount][columnCount].setText(rs.getString(recordCount));
                                          recordCount++;
                                     }
                                          rowCount++;
                                          recordCount = 1;
                                }
                           
                              if (!hasResults) JOptionPane.showMessageDialog(null, "Contact Not Found!", "Error", JOptionPane.WARNING_MESSAGE);
                              
                          }
            
                         catch (Exception err) 
                      {
                              System.out.println("ERROR: " + err);
                          }     
                 }
            Then in Class 2 I work with the result set object. Then I call Class1.closeConnections() to execute this:
                  public static void closeConnections() //giving null pointer
                 {
                      try
                      {
                           con.close();
                           rs.close();
                           s.close();
                      }
            
                      catch (Exception err)
                      {
                           System.out.println("ERROR: "+err);
                      }
                 }
            • 3. Re: Null Pointer when Closing JDBC Connection
              807569
              In databaseQuery, you have this:
              Connection con = DriverManager.getConnection(dbURL,"",""); 
              Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
              Because those variables are local to the method, the "static" variables are still null.

              You want to set values, not declare new variables:
              con = DriverManager.getConnection(dbURL,"",""); 
              s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
              I'm also not sure why your three variables are "static", and why the ResultSet rs is "public".
              • 4. Re: Null Pointer when Closing JDBC Connection
                807569
                //giving null pointer
                     {
                          try
                          {
                               con.close();
                Where is the 'con' initialized? Did you try printing out the reference at the concerned location(s) ?
                • 5. Re: Null Pointer when Closing JDBC Connection
                  807569
                  //giving null pointer
                       {
                            try
                            {
                                 con.close();
                  Where is the 'con' initialized? Did you try printing
                  out the reference at the concerned location(s) ?
                  The con is initialized at the very top with Connection con = DriverManager.getConnection(dbURL,"","");
                  • 6. Re: Null Pointer when Closing JDBC Connection
                    807569
                    The con is initialized at the very top with Connection con = DriverManager.getConnection(dbURL,"","");
                    Read my previous reply. You are not initializing the class variable, so the class variable accessed for the "close" is still null.
                    • 7. Re: Null Pointer when Closing JDBC Connection
                      807569
                      The con is initialized at the very top with
                      Connection con =
                      DriverManager.getConnection(dbURL,"","");

                      Read my previous reply. You are not initializing
                      the class variable, so the class variable accessed
                      for the "close" is still null.
                      I got it Ron, and I think it's working OK now. The reason those three variables are static is because the compiler gave errors that they needed to be.

                      Also, now when I try to close the resultset, it says it's already been closed. Does a resultset close itself when the last item in it is read?
                      • 8. Re: Null Pointer when Closing JDBC Connection
                        jschellSomeoneStoleMyAlias
                        Also, now when I try to close the resultset, it says
                        it's already been closed. Does a resultset close
                        itself when the last item in it is read?
                        You are closing them in the wrong order.

                        1. Close the result set.
                        2. Close the statement
                        3. Close the connection.