I am not new to java but just recently practicing about jdbc. I'm trying to fetch data from mysql database and load it to jtable..
here's my code for fetching the data class name FileMaintenance
public DefaultTableModel getProducts()
{
DefaultTableModel dtm = new DefaultTableModel();
try{
dtm.addColumn("Product Code");
dtm.addColumn("Description");
dtm.addColumn("Unit Price");
dtm.addColumn("Selling Price");
conn.stmt = conn.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql = "SELECT prodcode,proddesc,origprice,sellingprice FROM products";
rs = conn.stmt.executeQuery(sql);
while(rs.next()){
String code = rs.getString(1);
String desc = rs.getString(2);
String uprice = rs.getString(3);
String sprice = rs.getString(4);
dtm.addRow(new String[]{code,desc,uprice,sprice});
}
return dtm;
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Unable to fetch data...","Error",JOptionPane.ERROR_MESSAGE);
}
return null;
}
and there as the line that I used to call the method from FileMaintenance class
public void UpdateJTable()
{
DefaultTableModel model = new FileMaintenance().getProducts();
jtable.setModel(model);
}
now when I try to run the app, it says I'm connected to database which is true because I already tried inserting data and its successful, but there's an error occur. the error is related to DefaultTableModel because its point to line
DefaultTableModel model = new FileMaintenance().getProducts();
jtable.setModel(model);
it says:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at MyForm.UpdateJTable(MyForm.java:119) at MyForm$1.windowOpened(MyForm.java:104) at java.awt.Window.processWindowEvent(Window.java:2048) at javax.swing.JFrame.processWindowEvent(JFrame.java:296) at java.awt.Window.processEvent(Window.java:2009) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
could someone please tell me what is the problem with this code?