已解决java.lang.NumberFormatException异常的正确解决方法,亲测有效!!!已解决java.lang.NumberFormatException异常的正确解决方法,亲测有效! 我不是女神ヾ 2023-09-26 19:08 76阅读 0赞 已解决java.lang.NumberFormatException异常的正确解决方法,亲测有效!!! ![在这里插入图片描述][a98eec5618e9416f8adf82755122428f.webp_pic_center] #### 文章目录 #### * * 报错问题 * 解决方法 * 说明 ### 报错问题 ### 粉丝群里面的一个小伙伴敲代码时发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错信息如下: ![在这里插入图片描述][8f076594400b491986ca13af92b59b0c.jpeg_pic_center] 在调用StringUtils.split()和Integer.parseInt()时经常遇见这样的问题,不管传入的参数的值是什么,都能进入不为null或”“的判断中,然后就运行执行下面的代码,就可能出现 java.lang.NumberFormatException: For input string: "null"的异常,提示出现问题的位置在“Integer id = Integer.parseInt(idStr);”这是因为如果传入给Integer.parseInt()的值为空或者StringUtils.split()对空字符串进行切割无意义。如果传入的值不为null或“”可以正常运行不报错,如果为空值就会出现任如下异常。 by: java.lang.NumberFormatException:<u> </u>For input string: "null" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) 这里的For input string: “null” 并不是指传入的值为空,而是指传入的字符串为“null”,而“null”并不能被 StringUtils.split()切割,进而不能被Integer.parseInt()调用,所以会报错。 if(customerIdStr != null && !customerIdStr.equals("") ){ String[] customerIds = customerIdStr.split(","); //将字符串客户ID 转换为整数ID for (String idStr : customerIds) { Integer id = Integer.parseInt(idStr); customerRepository.updatefixedAreaId(fixedAreaId,id); } }else{ return; } ### 解决方法 ### 解决方法如下 ![在这里插入图片描述][51977e86019c4440b7d8465001aeac05.png] 所以只需要在上面的判断语句后面再加一个判断传入的参数是否不为“null”的条件即可解决此类异常问题。 if(customerIdStr != null && !customerIdStr.equals("") && !customerIdStr.equals("null")){ String[] customerIds = customerIdStr.split(","); ### 说明 ### 解决方法也是我在网上找的,如有侵权联系我删除 [a98eec5618e9416f8adf82755122428f.webp_pic_center]: https://img-blog.csdnimg.cn/a98eec5618e9416f8adf82755122428f.webp#pic_center [8f076594400b491986ca13af92b59b0c.jpeg_pic_center]: https://img-blog.csdnimg.cn/8f076594400b491986ca13af92b59b0c.jpeg#pic_center [51977e86019c4440b7d8465001aeac05.png]: https://img-blog.csdnimg.cn/51977e86019c4440b7d8465001aeac05.png
还没有评论,来说两句吧...