I am trying to insert a data from one of my jinternal frame to mysql database. I am using a resultset class method for the insert. I always end up having the NullPointerException even though the variables is not null. I can't pinpoint to where I am having this null exception in my code. Here is the snippet of actionListener for my jbutton:
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
String fabric = txtFabric.getText();
String supplier = txtSupplier.getText();
if(action.equals("Add")){
btnAdd.setText("Save");
btnAdd.setActionCommand("Save");
txtFabric.setEnabled(true);
txtFabric.grabFocus();
txtSupplier.setEnabled(true);
txtFabric.setText("");
txtSupplier.setText("");
}
else if(action.contentEquals("Save")){
if(fabric.isEmpty() || supplier.isEmpty()){
System.out.println("Nothing to save");
}
else{
System.out.println(fabric+" "+supplier);
try {
model.addFabric(fabric, supplier); // the error points to this line
} catch (SQLException e1) {
e1.printStackTrace();
}
}
btnAdd.setText("Add");
btnAdd.setActionCommand("Add");
txtFabric.setEnabled(false);
txtSupplier.setEnabled(false);
tableModel.fireTableDataChanged();
}
}
And here is the snippet of my class method insert:
public void addFabric(String item,String supplier) throws SQLException
{
try{
sql = "INSERT INTO fabric(ItemDesc,Supplier) values(?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, item);
pstmt.setString(2, supplier);
pstmt.executeUpdate();
}
catch(Exception ex){
ex.printStackTrace();
}finally{
pstmt.close();
con.close();
}
}
and here is the exception that I am getting pointing to the line of insertion:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Fabric.actionPerformed(Fabric.java:148)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)