Reputation: 21
I am currently working with an attendance management system, but when I click the "save" button this problem shows up 'java.lang.NumberFormatException:For input string:""' Here is my code:
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance","root","sydneydel");
String sql = "insert into attendance.student_info values(?,?,?,?,?)";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1,Integer.parseInt(jTextField2.getText()));
pst.setString(2, last.getText());
pst.setString(3,first.getText());
pst.setString(4,mid.getText());
String gender;
if (jRadioButton1.isSelected()){
gender=jRadioButton1.getText();
}
else{
gender=jRadioButton2.getText();
}
pst.setString(5, gender);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "SUCCESSFUL");
conn.close();
String data1= last.getText();
String data2= first.getText();
String data3= mid.getText();
String data4= gender;
Object[] row = {1, data2 + " " + data3 + " " + data1, data4};
model = (DefaultTableModel) table.getModel();
model.addRow (row);
first.setText("");
jTextField2.setText("");
last.setText("");
mid.setText("");
buttonGroup1.clearSelection();
if((last == null)&& (first==null)&& (buttonGroup1.equals(null))){
JOptionPane.showMessageDialog(null, "SAVE ERROR\nFill-up the information needed");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
How can I resolve my problem and how could I improve this program?
Upvotes: 0
Views: 2500
Reputation: 1
This is my first Answer ;)
String value = "10";
Integer convertedForm = Integer.parseInt(value);
This will works and the value is converted to Integer
because value is in number form
But if we pass ""
or "abc"
like that it can't convert from that to Integer
form because we are not passing number. So it will give NumberFormatException
.
So handle NumberFormatException
, if you pass other then number
Upvotes: 0
Reputation: 19655
parseInt
on the 4th line is receiving an empty string from jTextField2.getText()
, and of course that is not parseable as an integer.
Use a debugger to figure out why jTextField2.getText()
is blank. Is a number in fact entered in jTextField2
?
Upvotes: 1