我有一个数据库表如下:
CREATE TABLE "AcademicInfo" ("academicid" INTEGER PRIMARY KEY NOT NULL UNIQUE , "Title"
CHAR NOT NULL , "Forename" CHAR NOT NULL , "Surname" CHAR NOT NULL , "Role" CHAR NOT NULL,
"Room" CHAR NOT NULL , "Page" CHAR NOT NULL , "Hours" CHAR NOT NULL , "Phone" CHAR NOT NULL,
"Mobile" CHAR NOT NULL , "Email" CHAR NOT NULL , "Image" BLOB, "Password" CHAR NOT NULL )
下面的类:
public class MainMenu extends JFrame
{
Academic ac = new Academic();
Connection conn = JavaConnect.ConnectDB();
PreparedStatement pst = null;
String sql;
ResultSet rs = null;
/**
* Creates new form MainMenu
*/
public MainMenu() {
initComponents();
}
public void Update_table()
{
try {
String sqlStatement = "SELECT Title, Forename, Surname, Role FROM AcademicInfo ";
pst = conn.prepareStatement(sqlStatement);
rs = pst.executeQuery();
table_academic.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
finally {
try {
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(MainMenu.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
private void table_academicMouseClicked(MouseEvent evt)
{
try {
int row = table_academic.getSelectedRow();
String table_click = (table_academic.getModel().getValueAt(row, 2).toString());
String sqlSt = "SELECT * FROM AcademicInfo WHERE Surname='"+table_click+"'";
pst = conn.prepareStatement(sqlSt);
rs = pst.executeQuery();
if(rs.next()) {
label_name.setText(""+rs.getString("Surname"));
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
我有一个问题在' table_academicmouseclick '。我的目标是当一个记录被点击在一个表上,它得到确切的姓氏从数据库设置"label_name"适当,但我得到一个java.lang.NullPointerException在那个阶段。我只有一个问题与事件处理程序的方法,没有别的。我在academicmouseclick()做错了什么?
检查在initialize方法中是否初始化了label_name
。
label_name = new JLabel();