1
我有一個面板這樣的按鈕的代碼:無法保存到數據庫中,同時按下按鈕
try{
Koneksi objKoneksi = new Koneksi();
Connection kon = objKoneksi.bukaKoneksi();
Statement stat = kon.createStatement();
String query = "insert into transaksi(invoiceno,kode,nama,jumlah,harga,total) values('"
+Invoice.getText()+"','"
+Kode.getText()+"','"
+Nama.getText()+"','"
+Jumlah.getText()+"','"
+Harga.getText()+"','"
+Total.getText()+"','";
int rows = stat.executeUpdate(query);
if(rows==1){
JOptionPane.showMessageDialog(this,"Data sudah tersimpan");
kon.close();
}
}
catch(SQLException e){}
}
,也是一個Connection類:
,並聲明類:
(對不起,對不起在這裏發佈代碼。我有預格式化的文本塊上的一些麻煩)
當我按下按鈕,以將數據保存到數據庫,來這裏的錯誤:
Connection success:
You are not connected to database
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javafinalproject.Aplikasi.SaveActionPerformed(Aplikasi.java:184)
at javafinalproject.Aplikasi.access$200(Aplikasi.java:15)
at javafinalproject.Aplikasi$3.actionPerformed(Aplikasi.java:83)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)dd
和許多motre
有人可以幫助我?前 感謝
檢查它是否返回null或Connection對象objKoneksi.bukaKoneksi()。如果它返回null,那麼你必須調試objKoneksi.bukaKoneksi()這個方法 –
正如我在答案中所說的,一旦你打印你的異常,你將能夠看到什麼是錯的。我猜不出... – epoch
調試此行連接kon = objKoneksi.bukaKoneksi(); 。檢查這條線的kon執行值是多少 –