Reputation: 51
I have a code of a button from a panel like this :
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){}
}
And also a Connection class :
and statement class :
(Sorry for not post the code here. I had some trouble on preformatted text block)
When i hit the button to save the data to databases, here comes the error :
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
And Many motre
can someone help me with this? Thanks before
Upvotes: 1
Views: 79
Reputation: 16615
There is an exception occurring here:
catch (Exception ex) {
System.out.println("You are not connected to database");
return null;
}
You are suppressing it, you should show it via ex.printStackTrace()
.
Anyway, the method is then returning null
which is causing a NullPointerException
here:
Statement stat = kon.createStatement();
// --------------^^^
Update:
Put it here:
catch (Exception ex) {
System.out.println("You are not connected to database");
ex.printStackTrace();
return null;
}
PS: your best bet would be to go talk with your teacher/professor, it seems that you lack the basic understanding necessary to fix this issue.
Upvotes: 2