I cant see why i am getting errors, I believe i did everything correct. I only seem to to get errors when i try to get the text out the combobx. Can someone please hep me do this well. I know how to insert data from a text field to a data with microsoft access . But not when it comes to combo boxes.
Theses errors
java.lang.NullPointerException at delete_me1.connection(delete_me1.java:66) at delete_me1$2.actionPerformed(delete_me1.java:133) at java.awt.EventDispatchThread.run(Unknown Source) java.lang.NullPointerException at delete_me1.connection(delete_me1.java:90) at delete_me1$2.actionPerformed(delete_me1.java:133)
Thanks
Main Code
import java.awt.EventQueue; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JComboBox; import java.awt.BorderLayout; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JTextField; import javax.swing.JList; import javax.swing.AbstractListModel; 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 ; 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); } }