当我运行此代码时,我得到一个空指针异常。
try {
String sql = "INSERT INTO viwa(name,tele,surname) VALUES(?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(1, jTextField_name.getText());
pst.setString(2, jTextField_age.getText());
pst.setString(3, jTextField_tele.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Saved");
} catch (Exception e) {
e.printStackTrace();
}
*这是完整的代码*
`导入java.sql.Connection;导入 java.sql.PreparedStatement;导入java.sql.ResultSet;导入 java.sql.Statement;导入 javax.swing.JOptionPane;
公共类去扩展 javax.swing.JFrame {
Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; public go() { try { initComponents(); connect.getMyConn(); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jTextField_Tele = new javax.swing.JTextField(); jTextField_Surname = new javax.swing.JTextField(); jTextField_Name = new javax.swing.JTextField(); jButton_search = new javax.swing.JButton(); jButton_Update = new javax.swing.JButton(); jButton_Delete = new javax.swing.JButton(); jButton_save = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jLabel1.setText("Tele"); getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 140, -1, -1)); jLabel2.setText("Surname"); getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 210, -1, -1)); jLabel3.setText("Name"); getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 70, -1, -1)); jTextField_Tele.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField_TeleActionPerformed(evt); } }); getContentPane().add(jTextField_Tele, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 140, 170, -1)); getContentPane().add(jTextField_Surname, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 210, 170, -1)); getContentPane().add(jTextField_Name, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 70, 170, -1)); jButton_search.setText("Search"); getContentPane().add(jButton_search, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 310, 120, -1)); jButton_Update.setText("Update"); getContentPane().add(jButton_Update, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 310, 120, -1)); jButton_Delete.setText("Delete"); getContentPane().add(jButton_Delete, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 310, 120, -1)); jButton_save.setText("Save"); jButton_save.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton_saveActionPerformed(evt); } }); getContentPane().add(jButton_save, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 310, 120, -1)); pack(); }// </editor-fold> private void jTextField_TeleActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton_saveActionPerformed(java.awt.event.ActionEvent evt) { try { String sql = "INSERT INTO viwa (name,tele,surname) VALUES(?,?,?)"; pst = conn.prepareStatement(sql); pst.setString(1, jTextField_Name.getText()); pst.setString(2, jTextField_Tele.getText()); pst.setString(3, jTextField_Surname.getText()); pst.execute(); JOptionPane.showMessageDialog(this, "Saved"); } catch (Exception e) { e.printStackTrace(); } } `