title: java.lang.NullPointerException error message when I try to populate table in java swing with database data
body: <p>I tried to populate jTable in java swing with the data inside my database table. Here are the codes which I set up the connection.</p>
  
  <pre><code>    package DBController.database;
  
      import java.sql.*;
  
      public class DBController {
  private Connection con;
  
  public void setUp(String dsn) {
      try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");          
      } catch (Exception e) {
          System.out.println("Load driver error");
      }
      try {
  
          String s = "jdbc:odbc:" + dsn;
          con = DriverManager.getConnection(s, "", "");
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public ResultSet readRequest(String dbQuery) {
      ResultSet rs = null;
      try {
          Statement stmt = con.createStatement();
          rs = stmt.executeQuery(dbQuery);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return rs;
  }
  
  public int updateRequest(String dbQuery) {
      int count = 0;
      try {
          Statement stmt = con.createStatement();
          count = stmt.executeUpdate(dbQuery);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return count;
  }
  
  public void terminate() {
      try {
          con.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And here are the codes which I tried to write for the sql statement.</p>
  
  <pre><code>    public boolean retrieveDiscussion() {
      boolean success = false;
      ResultSet rs = null;
      DBController db = new DBController();
      db.setUp("IT Innovation Project");
      String dbQuery = "SELECT * FROM forumTopics WHERE topic_description = '" + description
              + "' and topic_category = '" + category
              + "' and topic_title = '" + title 
              + "' and topic_by = '" + postedBy + "'";
      rs = db.readRequest(dbQuery);
      db.terminate();
      return success;
  }
  </code></pre>
  
  <p>Here is the codes for my table in user interface.</p>
  
  <pre><code>    public static void main(String[] args) throws Exception {
      // The Connection is obtained
  
      eForumTopics topics = new eForumTopics();
      topics.retrieveDiscussion();
  
      // It creates and displays the table
      JTable table = new JTable(buildTableModel(null));
      JOptionPane.showMessageDialog(null, new JScrollPane(table));
  
      // Closes the Connection
  }
  public static DefaultTableModel buildTableModel(ResultSet rs)
  throws SQLException {
  
      ResultSetMetaData metaData = rs.getMetaData();
  
      // names of columns
      Vector&lt;String&gt; columnNames = new Vector&lt;String&gt;();
      int columnCount = metaData.getColumnCount();
      for (int column = 1; column &lt;= columnCount; column++) {
          columnNames.add(metaData.getColumnName(column));
      }
  
      // data of the table
      Vector&lt;Vector&lt;Object&gt;&gt; data = new Vector&lt;Vector&lt;Object&gt;&gt;();
      while (rs.next()) {
          Vector&lt;Object&gt; vector = new Vector&lt;Object&gt;();
          for (int columnIndex = 1; columnIndex &lt;= columnCount; columnIndex++) {
              vector.add(rs.getObject(columnIndex));
          }
          data.add(vector);
      }
  
      return new DefaultTableModel(data, columnNames);
  
      }
  </code></pre>
  
  <p>However, when I tried to run the application, it gave me </p>
  
  <pre><code>    java.lang.NullPointerException
  </code></pre>
  
  <p>error message. So what am I supposed to do? Thanks in advance.</p>