解決java.lang.StringIndexOutOfBoundsException: String index out of range: -1錯(cuò)誤問題
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
字符串截取下標(biāo)越界
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
出錯(cuò)代碼
result.put("value", valueBuilder.toString().substring(0,valueBuilder.toString().length()-1));修改后代碼
if (StringUtils.isNotBlank(valueBuilder.toString()) && valueBuilder.toString().length() >0){
result.put("value", valueBuilder.toString().substring(0,valueBuilder.toString().length()-1));
}心得:
- StringIndexOutOfBoundsException 異常源碼如下:
/**
* Thrown by {@code String} methods to indicate that an index
* is either negative or greater than the size of the string. For
* some methods such as the charAt method, this exception also is
* thrown when the index is equal to the size of the string.
*/
public
class StringIndexOutOfBoundsException extends IndexOutOfBoundsException {
private static final long serialVersionUID = -6762910422159637258L;
/**
* Constructs a {@code StringIndexOutOfBoundsException} with no
* detail message.
*
* @since JDK1.0.
*/
public StringIndexOutOfBoundsException() {
super();
}
/**
* Constructs a {@code StringIndexOutOfBoundsException} with
* the specified detail message.
*
* @param s the detail message.
*/
public StringIndexOutOfBoundsException(String s) {
super(s);
}
/**
* Constructs a new {@code StringIndexOutOfBoundsException}
* class with an argument indicating the illegal index.
*
* @param index the illegal index.
*/
public StringIndexOutOfBoundsException(int index) {
super("String index out of range: " + index);
}
}總共有以下幾個(gè)方法會(huì)拋出該異常:
String.substring() String.charAt() String.codePointAt() String.codePointBefore() String.subSequence() String.getChars() String.getBytes()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java TreeMap升序|降序排列和按照value進(jìn)行排序的案例
這篇文章主要介紹了Java TreeMap升序|降序排列和按照value進(jìn)行排序的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Spring Security OAuth2 token權(quán)限隔離實(shí)例解析
這篇文章主要介紹了Spring Security OAuth2 token權(quán)限隔離實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
spring boot中內(nèi)嵌redis的使用方法示例
這篇文章主要給大家介紹了關(guān)于spring boot中內(nèi)嵌redis使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
如何解決SpringBoot2.6及之后版本取消了循環(huán)依賴的支持問題
循環(huán)依賴指的是兩個(gè)或者多個(gè)bean之間相互依賴,形成一個(gè)閉環(huán),SpringBoot從2.6.0開始默認(rèn)不允許出現(xiàn)Bean循環(huán)引用,解決方案包括在全局配置文件設(shè)置允許循環(huán)引用存在、在SpringApplicationBuilder添加設(shè)置允許循環(huán)引用、構(gòu)造器注入2024-10-10
SpringBoot配置Access-Control-Allow-Origin教程
文章介紹了三種配置Spring Boot跨域訪問的方法:1. 使用過濾器;2. 在WebConfig配置文件中設(shè)置;3. 通過注解配置,作者分享了個(gè)人經(jīng)驗(yàn),并鼓勵(lì)讀者支持腳本之家2025-03-03
關(guān)于mybatis一對一查詢一對多查詢遇到的問題
這篇文章主要介紹了關(guān)于mybatis一對一查詢,一對多查詢遇到的錯(cuò)誤,接下來是對文章進(jìn)行操作,要求查詢?nèi)课恼?,并關(guān)聯(lián)查詢作者,文章標(biāo)簽,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費(fèi)消息的實(shí)例代碼
本文介紹了在SpringBoot中如何使用RabbitMQ進(jìn)行消息的生產(chǎn)和消費(fèi),詳細(xì)闡述了RabbitMQ中交換機(jī)的作用和類型,包括直連交換機(jī)、主題交換機(jī)、扇出交換機(jī)和頭交換機(jī),并解釋了各自的消息路由機(jī)制,感興趣的朋友一起看看吧2024-10-10
使用java?實(shí)現(xiàn)mqtt兩種常用方式
在開發(fā)MQTT時(shí)有兩種方式一種是使用Paho Java 原生庫來完成,一種是使用spring boot 來完成,這篇文章主要介紹了使用java?實(shí)現(xiàn)mqtt兩種方式,需要的朋友可以參考下2022-11-11

