public void actionPerformed(ActionEvent e) {
flag=0;
try{
String num,name,gender,address,phone,major;
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/university?characterEncoding=utf8″,”root”,”123456″);
num=t1.getText().trim();
ResultSet rs=sql.executeQuery(“SELECT * FROM student WHERE id='”+num+”‘”);
if(rs.next()){
name=rs.getString(“name”);
gender=rs.getString(“gender”);
address=rs.getString(“address”);
phone=rs.getString(“phone”);
major=rs.getString(“major”);
t2.setText(name);
t3.setText(gender);
t4.setText(address);
t5.setText(phone);
t6.setText(major);
if(e.getSource()==b2){
String temp=”DELETE FROM student WHERE id='”+num+”‘”;
sql.executeUpdate(temp);
JOptionPane.showMessageDialog(this,”删除成功!”,”提示对话框”,JOptionPane.INFORMATION_MESSAGE);
}
flag=1;
}else{
JOptionPane.showMessageDialog(this,”没有该学生!”,”提示对话框”,JOptionPane.INFORMATION_MESSAGE);
}
con.close();
if(flag==0){t1.setText(“没有该学生”);}
}
catch(SQLException ee){}
}
}
错误
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException: Cannot invoke “java.sql.Statement.executeQuery(String)” because “this.sql” is null
at GUI.StudentDelete.actionPerformed(StudentDelete.java:90)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityA