0

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.

    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();
    }
}

}

And here are the codes which I tried to write for the sql statement.

    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;
}

Here is the codes for my table in user interface.

    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<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    // data of the table
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

    }

However, when I tried to run the application, it gave me

    java.lang.NullPointerException

error message. So what am I supposed to do? Thanks in advance.

2
  • 3
    please give the full stack trace of the error i.e line number etc. Also create an SSCCE with random values etc, as we cannot test code for which we dont have the tables etc Commented Dec 20, 2012 at 11:19
  • Exception in thread "main" java.lang.NullPointerException at kioskeForum.ui.eForumDiscussion.buildTableModel(eForumDiscussion.java:271) at kioskeForum.ui.eForumDiscussion.main(eForumDiscussion.java:262) Commented Dec 20, 2012 at 12:09

1 Answer 1

1

Looks like the bug is

JTable table = new JTable(buildTableModel(null));
Sign up to request clarification or add additional context in comments.

2 Comments

But after I removed the null, the buildTableModel will have error. If I remove the ResultSet rs from public static DefaultTableModel buildTableModel, all the 'rs' after that will have error.
Actually I took the code from some website. These are the full codes. public static void main(String[] args) throws Exception { // The Connection is obtained ResultSet rs = stmt.executeQuery("select * from product_info"); // It creates and displays the table JTable table = new JTable(buildTableModel(rs)); JOptionPane.showMessageDialog(null, new JScrollPane(table)); // Closes the Connection } However,at the stmt there, the error message asked me cast something. I do not know how the cast Object thing works. Even I researched also no answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.