Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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!

 

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

 

@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

4 Replies
Anonymous
Not applicable
Author

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).
Anonymous
Not applicable
Author

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

Anonymous
Not applicable
Author

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.

Anonymous
Not applicable
Author

 

@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"