Reputation: 1276
I created a form to insert drug information to the db.
Only ItemID, ItemName and Expiry date is required. User can add other details if want.
My code for above functionality;
private void Save_btnActionPerformed(java.awt.event.ActionEvent evt) {
try {
String id = itemid_txt.getText();
String name = itemname_txt.getText();
String rec=reclevel_txt.getText();
String qty=qty_txt.getText();
String wp =wprice_txt.getText();
String sp =sprice_txt.getText();
Date expp =expday_chooser.getDate();
String dess = des_txtarea.getText();
String date = ((JTextField)dayChooser.getDateEditor().getUiComponent()).getText();
String exp = ((JTextField)expday_chooser.getDateEditor().getUiComponent()).getText();
String des = des_txtarea.getText();
if(id==null && name==null && exp==null){
JOptionPane.showMessageDialog(null, "Fill required fields(Item ID ,Item Name ,Exp Date) and Try again...");
}else if(wp!=null && sp!=null && rec!=null && qty!=null && exp==null){
String sql = "Insert into druginfo (ItemID,ItemName,AddedDate,RecorderLevel,InStock,WSPrice,CostPrice,ExpDate,Description)values (?,?,?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, id);
pst.setString(2, name);
pst.setString(3, date);
pst.setString(4, dess);
pst.setString(5, qty);
pst.setString(6, wp);
pst.setString(7, sp);
pst.setString(8, exp);
pst.setString(9, des);
}else{
pst.setString(1, id);
pst.setString(2, name);
pst.setString(3, date);
pst.setString(4, null);
pst.setString(5, null);
pst.setString(6, null);
pst.setString(7, null);
pst.setString(8, null);
pst.setString(9, des);
}
pst.execute();
JOptionPane.showMessageDialog(null, "New Item Data Saved...");
itemid_txt.setText(null);
itemname_txt.setText(null);
reclevel_txt.setText(null);
qty_txt.setText(null);
wprice_txt.setText(null);
sprice_txt.setText(null);
expday_chooser.setDate(null);
des_txtarea.setText(null);
} catch (SQLException ex) {
Logger.getLogger(InventoryManagementenew.class.getName()).log(Level.SEVERE, null, ex);
}
}
But not adding to the database when only required fields given and all fields are are given. Code throws java.lang.NullPointerException
.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.bit.project.InventoryManagementenew.Save_btnActionPerformed(InventoryManagementenew.java:468)
at com.bit.project.InventoryManagementenew.access$500(InventoryManagementenew.java:27)
at com.bit.project.InventoryManagementenew$6.actionPerformed(InventoryManagementenew.java:366)
line 468 is pst.setString(1, id);
.
Help me to correct this.
Upvotes: 0
Views: 478
Reputation: 104
Your preparedStatement object is referring to null as it is only initialized in if block of if-else-if construct. To solve the problem either initialize the preparedStatement outside if-else-if block or initialize it in each block if, elseif and else. One thing more you are not closing preparedStatement in your code this could lead to memory leak and will cause out of memory or too many connections to DB at higher rate of transactions in future.
Upvotes: 2
Reputation: 22972
State pst=conn.prepareStatement(sql);
before your if-else
conditions currently it will only be initialized in first else if
part otherwise pst
will be null
in subsequent else-if
and else
block.
Upvotes: 2
Reputation: 73558
You only initialize the PreparedStatement
in the else if
clause. The else
part has neither the sql string nor the prepared statement.
Upvotes: 3