0

当我按下搜索按钮将用户输入字段留空时,我收到以下错误页面:

java.lang.NumberFormatException: For input string: ""
    java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    java.lang.Integer.parseInt(Integer.java:470)
    java.lang.Integer.parseInt(Integer.java:499)

引发错误的代码是int nstudentid = Integer.parseInt(studentid);

4

1 回答 1

0

您试图从一个空字符串中解析一个数字,您应该添加一个检查:

//this means the number is not empty
if(studentid != null && !studentid.equals("")){
    int studentIdNum = Integer.parseInt(studentid);
}
else{
   //no number, deal with it
   throw new Exception("Invalid student Id!");
}
于 2012-08-07T13:54:55.173 回答