我有兩個JFrame
實例。主框架具有從數據庫中的數據填充到表,另一種是jFrame2而用戶添加或從主框架更新記錄表將自動刷新JTable不會刷新..我得到一個java.lang空指針異常
這裏是我的主框架代碼的代碼:
public void fillTable(){
String sql = "SELECT ID, UniqueCode, Name, Age, Gender, Cell FROM info";
try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(250);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(60);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(60);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(92);
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
,這裏是我的框架代碼2
private void save(){
try{
if(name.getText().isEmpty() || age.getText().isEmpty() || cell.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Empty Fields Are Not Allowed", "System Message", JOptionPane.WARNING_MESSAGE);
}
else if(" ".equals(cmbGender.getSelectedItem())){
JOptionPane.showMessageDialog(null, "Empty Fields Are Not Allowed", "System Message", JOptionPane.WARNING_MESSAGE);
}
else{
connect = mysqlconnect.DBConnect();
stmt = connect.createStatement();
rs = stmt.executeQuery("SELECT Name FROM info WHERE Name='" + name.getText() + "'");
if(rs.first()){
JOptionPane.showMessageDialog(null, "Name already Exist, Duplicate info is not allowed", "System Message", JOptionPane.WARNING_MESSAGE);
}else{
sql = "INSERT INTO info(UniqueCode, Name, Age, Gender, Cell) values ('" + uniqueID.getText()+ "', '" + name.getText() + "', '" + age.getText() + "', " +
" '" + cmbGender.getSelectedItem() + "', '" + cell.getText() + "') ";
Statement st = connect.createStatement();
st.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Successfully Saved", "System Message", JOptionPane.INFORMATION_MESSAGE);
this.dispose();
frame.jTable1.repaint(); <====== Error comes here because i want to refresh my table
}
}
}
catch(Exception ex){
System.out.println(ex);
}
1)爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 2)請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) – 2013-05-03 13:28:08
拋出NullPointerException的位置在哪裏?你應該提供更多的細節。 – Kai 2013-05-03 13:29:51
請勿使用多個「JFrames」。發佈例外,以便我們能夠弄清楚什麼是錯的。 – Amarnath 2013-05-03 13:44:39