How to interpret a
java.lang.StringIndexOutOfBoundsException: length=30278; regionStart=6877; regionLength=-12
or better how to reproduce such an exception where
length
is positive, regionStart
is positive and less than length
regionLength
is negative?What do these attributes mean at all? They aren't mentioned in the documentation of this exception.
I only know java.lang.StringIndexOutOfBoundsException: String index out of range: X
where X
is a negative integer.
Reproducable with:
String s = "hello world";
String chunk = s.substring(5, -8);
for example.
Asking because such an exception is thrown in my android app. But it is thrown very seldom. Otherwise I would set a breakpoint and see what is going on there.
java.lang.StringIndexOutOfBoundsException: length=30278; regionStart=6877; regionLength=-12
at java.lang.String.startEndAndLength(String.java:298)
at java.lang.String.substring(String.java:1087)
...
my app specific calls here
...
at android.view.View.performClick(View.java:5217)
at android.view.View$PerformClick.run(View.java:21342)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
As I know what action can lead to this exception I've set a breakpoint at that position and opened String
class definition (because Android uses analogs of standard java classes sometimes) when the debugger stopped there. But the String
class didn't contain startEndAndLength
method...
Ok. I was able to catch the exception and can now deduce the meaning of the attributes mentioned. So if you have an instruction like:
text.substring(start, end)
length
will be the length of the text
(or text.length()
)
regionStart
will be start
and regionLength
will be (end - start
)
When I look at it now, it looks simple, but if you see it first time it's not so clear as you usually see another message when StringIndexOutOfBoundsException
is thrown.