0

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?

2
  • 3
    the problem is that an empty string is not a valid numerical value. here's another one: buttonGroup1.equals(null) -> this is a nullPointerException in the making. if buttonGroup1 is null, this will crash Commented Nov 8, 2019 at 9:52
  • I assume this: pst.setInt(1,Integer.parseInt(jTextField2.getText())); is where you get the error? check if jTextField2 is empty, or provide a default value Commented Nov 8, 2019 at 9:53

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.