简体   繁体   中英

java.lang.NullPointerException error message when I try to populate table in java swing with database data

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.

看起来错误是

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM