解决问题:java.lang.NumberFormatException: For input string: "null"

    在调用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: 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;
		}
所以只需要在上面的判断语句后面再加一个判断传入的参数是否不为“null”的条件即可解决此类异常问题。
if(customerIdStr != null && !customerIdStr.equals("") && !customerIdStr.equals("null")){
			String[] customerIds = customerIdStr.split(",");

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值