2013-01-20 108 views
0

當我運行此代碼時,我得到一個空指針異常。java INSERT查詢不起作用(java.lang.NullPointerException)

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中; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import 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(); 
    } 

}  ` 
+1

你在哪一行得到空指針異常?在你調用'prepareStatement()'方法之前,你的連接變量'conn'被初始化,即'pst = conn.prepareStatement(sql);' 爲了更加清晰,添加完整的代碼片段。 –

回答

0

使用你的IDE,將斷點這段代碼的開始,並通過它一步看所有變量。其中一個變量包含意想不到的null值。在你的代碼中沒有其他明顯的NPE原因。