numberformatexception是什么異常
這個異常是說,在將字符串轉換為number的時候格式化錯誤造成的,解決方法如下:
1、SQLException:由于操作數據庫所產生的異常。
2、NullPointerException:空指針異常,當你指向一個沒有元素的坐標時會產此異常。
3、NoSuchFieldException:字段未找到異常。
4、NoSuchMethodError:方法未找到異常。經常是由于導包錯誤或者誤刪導致方找不到。
5、最后NumberFormatException:由于字符串類型轉化為數字類型異常所拋出的異常。
java.lang.NumberFormatException報錯
NumberFormatException異常什么情況下出現?
java.lang.NumberFormatException: For input string: ""怎么解決?
可以這樣:Double.parDouble(n.equals("")?"0":n);
注:n是你需要轉換的字符串。
因為:java.lang.NumberFormatException: For input string: " "
這個異常是說,在將字符串轉換為number的時候格式化錯誤。
“”空的字符串有對應的數值嗎,這里顯然沒有,所以就一個問題,如上即可。
擴展資料:注意事項
如果傳入的值不為null或“”可以正常運行不報錯,如果為空值就會出現任如下異常。
by: java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parInt(Integer.java:580)
at java.lang.Integer.parInt(Integer.java:615)
這里的
For input string: "null"
并不是指傳入的值為空,而是指傳入的字符串為“null”,而“null”并不能被StringUtils.split()切割,進而不能被Integer.parInt()調用,所以會報錯。
if(customerIdStr != null && !customerIdStr.equals("") ){
String[] customerIds = customerIdStr.split(",");
//將字符串客戶ID 轉換為整數ID
for (String idStr : customerIds) {
Integer id = Integer.parInt(idStr);
customerRepository.updatefixedAreaId(fixedAreaId,id);
}
}el{
return;
}
所以只需要在上面的判斷語句后面再加一個判斷傳入的參數是否不為“null”的條件即可解決此類異常問題。
if(customerIdStr != null && !customerIdStr.equals("") && !customerIdStr.equals("null")){
String[] customerIds = customerIdStr.split(",")
NumberFormatException問題求教。急!
本文發布于:2023-02-28 19:11:00,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/167760240755660.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:numberformatexception.doc
本文 PDF 下載地址:numberformatexception.pdf
| 留言與評論(共有 0 條評論) |