当我尝试将数据加载到JTable中时出现以下错误。
线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException 在com.tfc.cheque.handle.dao.impl.ChequeDAOImpl.getCheques(ChequeDAOImpl.java:23) 在com.tfc.cheque.handle.ui.ChequeGUI。(ChequeGUI.java:38) 在TestChequeDAO $ 1.run(TestChequeDAO.java:30) 在java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
这是我的主要课程:
public class TestChequeDAO {
public static void main(String[] args) throws SQLException{
DBConnection connection = new DBConnection();
connection.setUser("");
connection.setPswd("");
ChequeDAOImpl dao = new ChequeDAOImpl();
dao.setConnection(connection);
List<Cheque> cheques = dao.getCheques();
EventQueue.invokeLater(new Runnable() {
public void run() {
ChequeGUI cgui;
try {
cgui = new ChequeGUI();
cgui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
cgui.pack();
cgui.setVisible(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
这是我的gui课程:
public class ChequeGUI extends JFrame {
JTable guiTable = new JTable();
DefaultTableModel model = new DefaultTableModel(new Object[][]{},new String[]{"Name","Amount","Date"});
public ChequeGUI() throws SQLException {
guiTable.setModel(model);
add(new JScrollPane(guiTable));
//Populate Table
ChequeDAOImpl chqdi = new ChequeDAOImpl();
List<Cheque> cheques = chqdi.getCheques();
for(Cheque cq : cheques){
model.addRow(new Object[]{cq.getName(),cq.getAmount(),cq.getDate()});
}
}
}
这是我编写查询的地方:
public class ChequeDAOImpl implements ChequeDAO{
//DB2 connection
private DBConnection dbConnection;
public List<Cheque> getCheques() throws SQLException{
List<Cheque> cheques = new ArrayList<Cheque>();
Connection connection = dbConnection.getConnection();
try{
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("select * from LIB.DATA");
while(result.next()) {
Cheque cheque = new Cheque();
cheque.setAmount(result.getDouble("AMOUNT"));
cheque.setDate(result.getDate("DATE"));
cheque.setName(result.getString("NAME"));
cheques.add(cheque);
for(Cheque chq : cheques){
System.out.println("Name: " + result.getString("NAME"));
System.out.println("Amount: " + result.getDouble("AMOUNT"));
System.out.println("Date: " + result.getDate("DATE"));
}
}
}catch(Exception exception){
exception.printStackTrace();
}finally{
connection.close();
}
return cheques;
}
public DBConnection getConnection() {
return dbConnection;
}
public void setConnection(DBConnection connection) {
this.dbConnection = connection;
}
}
答案 0 :(得分:0)
您需要初始化DBConnection dbConnection
,这在很大程度上取决于您的应用程序的工作方式