Android Question StringIndexOutOfBoundsException

Discussion in 'Android Questions' started by xunwichtig, Feb 10, 2015.

  1. xunwichtig

    xunwichtig Member Licensed User

    Can anyone explain me, why this will crash, btw. why the len-check (If TEndIDX >= TLen Then) dosn't work?
    The if was never execute, btw. looks like ignored.

    Code:
    Dim TData As String = File.ReadString(File.DirRootExternal, "data.dat")
    Dim TLen As Int = File.Size(File.DirRootExternal, "data.dat")
    Log("TDataLen:" & TData.Length)
    Dim X As Int
    Dim TStep As Int = 4000
    Dim TEndIDX As Int
    Dim T As String
    For X = 0 To TLen - 1 Step TStep
       TEndIDX = X + TStep
       
    If TEndIDX >= TLen Then
         TEndIDX = TLen - 
    1
       
    End If
       
    Log("FROMTO:" & X & " - " & TEndIDX & " - " & TLen)
       T = TData.SubString2(X, TEndIDX)  
    '!!!!!!!! Crashs here
       Log("T:" & T.Length)
    Next
    Code:
    ...
    T:
    4000
    FROMTO:
    160000 - 164000 - 176719
    T:
    4000
    FROMTO:
    164000 - 168000 - 176719
    Error occurred on line: 
    123 (xxx)
    java.lang.StringIndexOutOfBoundsException: length=
    164339; regionStart=164000; regionLength=4000
       at java.lang.String.startEndAndLength(
    String.java:588)
       at java.lang.String.substring(
    String.java:1453)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:
    515)
       at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:636)
       at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:302)
       at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:238)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:
    515)
       at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    121)
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    163)
       at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    159)
       at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:77)
       at android.view.View.performClick(
    View.java:4658)
       at android.view.View$PerformClick.run(
    View.java:19461)
       at android.os.Handler.handleCallback(Handler.java:
    733)
       at android.os.Handler.dispatchMessage(Handler.java:
    95)
       at android.os.Looper.loop(Looper.java:
    146)
       at android.app.ActivityThread.main(ActivityThread.java:
    5653)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:
    515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    1291)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    1107)
       at dalvik.system.NativeStart.main(Native Method)

    JimJim
    XUN
     
  2. sorex

    sorex Expert Licensed User

    this works fine here, I had different values for file.size & string.lenght so I changed it to what I had as string.

    Code:
    Dim TData As String = File.ReadString(File.DirAssets, "data.dat")
    Dim TLen As Int = tdata.Length
    Dim X As Int
    Dim TStep As Int = 4000
    Dim TEndIDX As Int
    Dim T As String
    For X = 0 To TData.Length Step TStep
       TEndIDX = X + TStep
       
    If TEndIDX >= TData.Length Then
         TEndIDX = TData.Length
       
    End If
       
    Log("FROMTO:" & X & " - " & TEndIDX & " - " & TLen)
       T = TData.SubString2(X, TEndIDX)  
    '!!!!!!!! Crashs here
       Log("T:" & T.Length)
        
    Log(T)
    Next
     
  3. xunwichtig

    xunwichtig Member Licensed User

    o_O ... why the hell the len's aren't the same?
     
  4. sorex

    sorex Expert Licensed User

    don't know, the filesize one was even 0 here while it just read the file without problems.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice