4

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
  • and 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...

Pshemo
  • 122,468
  • 25
  • 185
  • 269
ka3ak
  • 2,435
  • 2
  • 30
  • 57
  • 1
    @baao That is not a duplicate of this question – Mark Rotteveel May 19 '18 at 13:23
  • 1
    I don't see any `regionLength` in [String class](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/String.java) nor StringIndexOutOfBoundsException class, which suggests that it was created via `throw new StringIndexOutOfBoundsException("length=" + length + ", regionStart=" + regionStart + ", regionLength=" + regionLength);` outside of that class. From where it was actually thrown? Can you provide some [mcve] and stacktrace? – Pshemo May 19 '18 at 13:31
  • 1
    Catch it and print its stack trace to see where it's thrown. – rustyx May 19 '18 at 13:44

2 Answers2

0

As define in the method definition If the regionstart is bigger than regionend you will got this exception.

CodeScale
  • 3,046
  • 1
  • 12
  • 20
0

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.

ka3ak
  • 2,435
  • 2
  • 30
  • 57