2013-08-06 34 views
-1

我不明白爲什麼我得到的錯誤,我相信我做了一切正確的。當我嘗試將文本從combobx中取出時,我似乎只會出錯。有人能幫助我做到這一點。我知道如何將文本字段中的數據插入到具有Microsoft Access的數據中。但是,當涉及到組合框。Java/Sql的GUI PreparedStatement

論文錯誤

顯示java.lang.NullPointerException 在delete_me1.connection(delete_me1.java:66) 在delete_me1 $ 2.actionPerformed(delete_me1.java:133) 在java.awt.EventDispatchThread.run(來源不明)在delete_me1.connection(delete_me1.java:90 顯示java.lang.NullPointerException ) 在delete_me1 $ 2.actionPerformed(delete_me1.java:133)

謝謝:)

import java.awt.EventQueue; 

public class delete_me1 { 

    private JFrame frame; 
    public JComboBox<?> comboBox; 

    String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim(); 
    String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb"; 

    Connection con = null; 
    Statement st = null; 
    ResultSet rs; 
    private JTextField textField; 

    /** 
    * Launch the application. 
    */ 



    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        delete_me1 window = new delete_me1(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 



    public void connection() throws Exception { 


     /* String sql = "INSERT INTO animal (first_name, last_name) VALUES(?,?)"; 
     PreparedStatement preparedStatement = connection.prepareStatement(sql); 
     preparedStatement.setString(1,animal); 
     preparedStatemnt.setString(2,animalType); 
     preparedStatemnt.executeUpdate();*/ 

     try { 

      String com2 = comboBox.getSelectedItem().toString(); 
      String com = textField.getText(); 
      String sql = "INSERT INTO client (first_name, last_name) VALUES (?,?)"; 
      PreparedStatement preparedStatement = con.prepareStatement(sql); 
      preparedStatement.setString(1,com); 
      preparedStatement.setString(2,com2); 
      preparedStatement.executeUpdate(); 


      Class.forName(MSAccessDriver); 
      String db = "jdbc:odbc:Food"; // db = database string stored 
              // in the database 
      con = DriverManager.getConnection(db); 
      st = con.createStatement(); 
      st.execute(sql); 

     } 

     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     finally { 
      try { 
       st.close(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       con.close(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      JOptionPane.showMessageDialog(null, "Save Complete Successfully"); 
     } 

    } 

    /** 
    * Create the application. 
    */ 
    public delete_me1() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     final JComboBox comboBox = new JComboBox(); 
     comboBox.setModel(new DefaultComboBoxModel(new String[] { "Dog", "Cat", 
       "Cow", "Sheep" })); 
     comboBox.setBounds(57, 54, 245, 46); 
     frame.getContentPane().add(comboBox); 

     JButton btnNewButton = new JButton("New button"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       try { 
        connection(); 

       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
     btnNewButton.setBounds(57, 143, 89, 23); 
     frame.getContentPane().add(btnNewButton); 

     textField = new JTextField(); 
     textField.setBounds(172, 144, 118, 20); 
     frame.getContentPane().add(textField); 
     textField.setColumns(10); 
    } 
} 
+3

''sun.jdbc.odbc.JdbcOdbc「.trim();'爲什麼?另外,有什麼錯誤? –

+1

你必須初始化'con'才能使用它 – jongusmoe

+0

我做了「Connection con = null;」 –

回答

0

換句話說,你的問題是「什麼是空指針異常?」當程序中的子程序的先決條件假定變量在使用該變量時不爲null時,會發生什麼情況。

現在讀取您的錯誤,「delete_me1.connection」是哪裏應該沒有空值的地方。