エラーが発生する理由がわかりません。すべて正しいことをしたと思います。テキストをコンボボックスから取得しようとすると、エラーが発生するようです。誰かが私がこれをうまくやるのを手伝ってくれませんか。Microsoft Access を使用して、テキスト フィールドからデータにデータを挿入する方法を知っています。しかし、コンボボックスに関してはそうではありません。
論文のエラー
java.lang.NullPointerException at delete_me1.connection(delete_me1.java:66) at delete_me1$2.actionPerformed(delete_me1.java:133) java.awt.EventDispatchThread.run で (ソース不明) delete_me1.java:90) で 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);
}
}