我正在尝试将一个数据从我的jinternal框架插入到mysql数据库中。我正在使用结果集类方法进行插入。即使变量不为null,我总是会得到NullPointerException。我无法确定我的代码中出现此null异常的位置。这是我的jbutton的actionListener片段:
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();
}
}
这是我的类方法插入的片段:
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();
}
}
以下是我指向插入行的例外情况:
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)