【解决问题.思路篇】StringIndexOutOfBoundsException:String index out of range: -1
2015-05-23 22:15
691 查看
看到题目,就应该能想到应该是字符串过长引起的问题。下面咱们分析一下。
报错:
根据代码跟踪,发现是首页数据加载完之后就会报错,所以继续跟踪,发现了问题。
当我们输入了访问地址:localhost:8080页面以及数据加载完就会报错,抛异常。但是不影响运行。后面一步步跟踪代码,终于找到问题的根本所在了。
其中一个jsp页面是这样写的:
发现获取Ip获取后的值为0:0:0:0:0:0:0:1。Ip的值太长了,于是把indexOf(".")改为indexOf(":")还报错:0:0:0:1,还是太长了。于是发现这是因为访问地址的路径为localhost:8080,修改为127.0.0.1:8080就可以了。
总结
其实很多时候,当我们积累足够的时候,就能根据报错找出大概是什么问题,然后根据问题顺藤摸瓜就能找到问题的所在。解决问题的思路这也是一种经验,多积累。
报错:
严重: Servlet.service()for servlet jsp threw exception java.lang.StringIndexOutOfBoundsException:String index out of range: -1
根据代码跟踪,发现是首页数据加载完之后就会报错,所以继续跟踪,发现了问题。
当我们输入了访问地址:localhost:8080页面以及数据加载完就会报错,抛异常。但是不影响运行。后面一步步跟踪代码,终于找到问题的根本所在了。
其中一个jsp页面是这样写的:
//获取IP地址 StringIp=request.getRemoteAddr(); String ip1,ip2,ip3,ip4; int i=Ip.indexOf("."); ip1=Ip.substring(0,i); Ip=Ip.substring(i+1,Ip.length()); i=Ip.indexOf("."); ip2=Ip.substring(0,i); Ip=Ip.substring(i+1,Ip.length()); i=Ip.indexOf("."); ip3=Ip.substring(0,i); ip4=Ip.substring(i+1,Ip.length()); String Ipone=ip1; StringIptwo=ip1+"."+ip2;
发现获取Ip获取后的值为0:0:0:0:0:0:0:1。Ip的值太长了,于是把indexOf(".")改为indexOf(":")还报错:0:0:0:1,还是太长了。于是发现这是因为访问地址的路径为localhost:8080,修改为127.0.0.1:8080就可以了。
总结
其实很多时候,当我们积累足够的时候,就能根据报错找出大概是什么问题,然后根据问题顺藤摸瓜就能找到问题的所在。解决问题的思路这也是一种经验,多积累。
相关文章推荐
- mvn install:java.lang.StringIndexOutOfBoundsException: String index out of range: -1问题的解决
- eclipse Java文件打开异常解决方法 :java.lang.StringIndexOutOfBoundsException: String index out of range: 26
- java.lang.StringIndexOutOfBoundsException: String index out of range: 1这是怎么回事?
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误的原因 3ff0
- Hibernate query.list()查询发生java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误
- Hibernate query.list()查询发生java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误
- spark将数据保存到elasticsearch,老是爆java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- Spring Boot 启动报错 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0
- selenium cant start a new browser(ie7):java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- 解决ava.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)类似问题
- java.lang.IllegalArgumentException:pointerIndex out of range问题的两种解决办法
- java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 21
- java.lang.IllegalArgumentException: pointerIndex out of range 问题的两种解决办法
- java.lang.IllegalArgumentException: pointerIndex out of range 问题的两种解决办法
- DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法
- DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法