java.lang.NullPointerException当我尝试使用数据库数据填充java swing中的表时出现错误消息

时间:2012-12-20 11:17:50

标签: java sql swing

我尝试使用数据库表中的数据在java swing中填充jTable。以下是我建立连接的代码。

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

}

以下是我尝试为sql语句编写的代码。

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

以下是用户界面中我的表格的代码。

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

    }

然而,当我试图运行该应用程序时,它给了我

    java.lang.NullPointerException

错误消息。那我该怎么办?提前谢谢。

1 个答案:

答案 0 :(得分:1)

看起来好像是

JTable table = new JTable(buildTableModel(null));