错误检查 int 值的空 textField

error check the empty textField of int values

代码如下:

String name = txtname.getText();
         int id = Integer.parseInt(txtid.getText());
        int pass = Integer.parseInt(txtpass.getText());
         String day = Date.getSelectedItem().toString();


    if (txtname.getText().isEmpty() ||txtid.getText().isEmpty()||Date.getSelectedIndex()==0 || event == null||time==null) {


        JOptionPane.showMessageDialog(null, "you should add all information", "error", JOptionPane.ERROR_MESSAGE);



    }                                       

    else {

        ((DefaultListModel) jList1.getModel()).addElement(txtname.getText() + " " + txtpass.getText() + "(" + Date.getSelectedItem() + ") "+event+", "+time);
   try{
               Statement stmt = con.createStatement();
                stmt.execute("INSERT INTO company(name,id) VALUES('"+name+"',"+id+");");
                stmt.execute("INSERT INTO events(pass,time,status,day,companyId) VALUES("+pass+",'"+time+"','"+event+"','"+day+"',"+id+");");

               JOptionPane.showMessageDialog(this, "Record Submit");

          }catch(Exception ex){
              JOptionPane.showMessageDialog(this, ex);

             } 


    }


    this.txtname.setText(null);
    this.txtpass.setText(null);
    this.txtid.setText(null);
    this.Date.setSelectedIndex(0);
    this.jCheckBox1.setSelected(false);
    this.jCheckBox2.setSelected(false);
    this.jCheckBox3.setSelected(false);


}

这是错误:

java.lang.NumberFormatException: For input string: ""

该代码可能适用于字符串文本字段,但不适用于整数文本字段。
如果我删除它可能会起作用:

 txtid.getText().isEmpty()

请告诉我如何修复代码?

似乎 txtid.getText() 正在返回“”。那是对的吗? 字符串 "" 不是数字,无法被 Integer.parseInt() 解析。你可以做的一件事是尝试,抓住。如果它抛出 NumberFormatException 错误,您可以相应地处理它。