数值格式异常
1.异常的原因:
查找到了异常是出现在 Utils 类中的 paseInt() 方法中,在 String 类在转换成 Int 类时,存在转换失败或空值的隐患,源代码如下:
public static int paseInt(String intStr) {
int result = 0;
try {
result = (intStr);
} catch (Exception e) {
();
}
return result;
}
2.解决方案:
为了消除转换该隐患,并更好地利用这个工具类方法,可以再传入一个默认的 Int 类型参数,当转换失败或为 null 时,使用默认参数来代替转换失败的值,代码如下:
public static int paseInt(String intStr, int defaultValue) {
int result = 0;
try {
result = (intStr);
} catch (Exception e) {
return defaultValue;
}
return result;
}
3.测试
测试之后,: null 的异常没有再出现。再开发中,当调用 paseInt() 方法时,如果传入的 intStr 并没有被转化成功为一个 int 型值,那么该方法会返回那个传入的 默认值,所以,该默认值应当根据开发的需求来设定好,例如分页功能中,可以将默认值设定为1.