StringIndexOutOfBoundsException: String index out of range: 40 how to fix

Highlighted
Seven Stars

StringIndexOutOfBoundsException: String index out of range: 40 how to fix

I have two columns (tmysqloutput) that I will get the value from only one column (tmysqlinput) depends on the condition, when I tried  to run with to one column first, everything are working perfectly, but then when I load the expression on the second column, I got this error:

 

Exception in component tMap_4 (FileExtractAndLoad)
java.lang.StringIndexOutOfBoundsException: String index out of range: 40
at java.lang.String.substring(Unknown Source)

 

My expression syntax for the column that throwing that error is:

 

row5.ADDRESS.contains("%TOSL%") ? row5.ADDRESS.substring(0,4): ((row5.ADDRESS.substring((row5.ADDRESS.indexOf("to") + 3),(row5.ADDRESS.indexOf("to") + 10))).replace("-", "").replaceAll("\\D*","")) 

 

While the first one is this:

 

row5.ADDRESS.contains("%Refer%") ? row5.ADDRESS.substring(0,4): ((row5.ADDRESS.substring((row5.ADDRESS.indexOf("ABC") + 4),(row5.ADDRESS.indexOf("ABC") + 11))).replace("-", "").replaceAll("\\D*","")) 

 

 

 

Is there any possible way on how ti fix this? And if there's a way, I need to implement it on both expression?

 

Thanks!

 


Accepted Solutions
Seven Stars

Re: StringIndexOutOfBoundsException: String index out of range: 40 how to fix

 

@DVSCHWAB

Thanks for taking time to response on my question, I fixed this by doing the code below:

 

row5.ADDRESS.contains("%REFER%") ? row5.ADDRESS.trim(): ((row5.ADDRESS.substring(row5.ADDRESS.indexOf("ABC") +4, Math.min(row5.ADDRESS.indexOf("ABC") + 11, row5.ADDRESS.length())).replace("-","").replaceAll("\\D",""))) 

 

 

The key is to use the "Math.min and length"

 

 

View solution in original post


All Replies
Eight Stars

Re: StringIndexOutOfBoundsException: String index out of range: 40 how to fix

In the second expression you need to change row5.ADDRESS.substring(0,4) to row5.ADDRESS.substring(0,5), since the other numbers all increased by 1 (i.e 3 to 4, and 10 to 11).
Seven Stars

Re: StringIndexOutOfBoundsException: String index out of range: 40 how to fix

What do you mean? Sorry, but is that a serious answer?

Eight Stars

Re: StringIndexOutOfBoundsException: String index out of range: 40 how to fix

Yes, although on looking at your post again, I think I switched the expression that works with the one that doesn't. The one that works is:

 

row5.ADDRESS.contains("%Refer%") ? row5.ADDRESS.substring(0,4): ((row5.ADDRESS.substring((row5.ADDRESS.indexOf("ABC") + 4),(row5.ADDRESS.indexOf("ABC") + 11))).replace("-", "").replaceAll("\\D*",""))

 

correct? And for the second expression, you copied this and updated the "index numbers", right?

 

row5.ADDRESS.contains("%TOSL%") ? row5.ADDRESS.substring(0,4): ((row5.ADDRESS.substring((row5.ADDRESS.indexOf("to") + 3),(row5.ADDRESS.indexOf("to") + 10))).replace("-", "").replaceAll("\\D*",""))

 

I think the issue is with row5.ADDRESS.substring(0,4); the row5.ADDRESS.contains("%TOSL%") can return a string of length 3 ("TOSL"), so it should be row5.ADDRESS.substring(0,3). Otherwise, when it returns "TOSL", you're trying to read position 4 in a zero-indexed string, which is out of bounds.

 

Hope this helps.

Seven Stars

Re: StringIndexOutOfBoundsException: String index out of range: 40 how to fix

 

@DVSCHWAB

Thanks for taking time to response on my question, I fixed this by doing the code below:

 

row5.ADDRESS.contains("%REFER%") ? row5.ADDRESS.trim(): ((row5.ADDRESS.substring(row5.ADDRESS.indexOf("ABC") +4, Math.min(row5.ADDRESS.indexOf("ABC") + 11, row5.ADDRESS.length())).replace("-","").replaceAll("\\D",""))) 

 

 

The key is to use the "Math.min and length"

 

 

View solution in original post

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog