Android Question Error When reading data

Alhootti

Active Member
why this error appeared:
B4X:
b4xmainpage_serial_dataavailable (java line: 196)
java.lang.StringIndexOutOfBoundsException: length=0; index=2
    at java.lang.String.substring(String.java:2060)
    at b4a.a3tvmanager3.b4xmainpage._serial_dataavailable(b4xmainpage.java:196)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7664)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
im using this code:
B4X:
Private Sub serial_DataAvailable (Buffer() As Byte)
    ReadData  = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
    If ReadData.StartsWith("$GNZDA") Then
    Dim parts() As String = Regex.Split(",", ReadData)
    Dim Time As String = parts(1)
    Dim hours As String = Time.SubString2(0, 2)' + 04
    Dim minutes As String = Time.SubString2(2, 4)
        Dim seconds As String = Time.SubString2(4, 6)
        Dim DateGPS As String = parts(2)
        Dim DayGPS As String = DateGPS.SubString2(0, 2)
        Dim DateGPS2 As String = parts(3)
        Dim MonthGPS As String = DateGPS2.SubString2(0, 2)
        Dim DateGPS3 As String = parts(4)
        Dim YearGPS As String = DateGPS3.SubString2(0, 4)
'        Label2.Text = DayGPS & "/" & MonthGPS & "/" & YearGPS
        Dim ConvertedTime As Long = DateUtils.SetDateAndTime2(YearGPS, MonthGPS, DayGPS, hours, minutes, seconds, 0)
        DateTime.TimeFormat = "hh:mm:ss"
        Label1.Text = (DateTime.Time(ConvertedTime))' & " " & DateTime.time(MyDate))
'    Label1.Text = hours & ":" & minutes & ":" & seconds
    End If
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
java.lang.StringIndexOutOfBoundsException: length=0; index=2
you cannot assume that a string has a given length. you have to test for its length before performing
any operations on it. in this case, the string is empty (length = 0), therefore any substring operations
will throw an exception.

1 (or, probably all) of these strings is empty:
Dim Time As String = parts(1)
Dim hours As String = Time.SubString2(0, 2)' + 04
Dim minutes As String = Time.SubString2(2, 4)
Dim seconds As String = Time.SubString2(4, 6)
Dim DateGPS As String = parts(2)
Dim DayGPS As String = DateGPS.SubString2(0, 2)
Dim DateGPS2 As String = parts(3)
Dim MonthGPS As String = DateGPS2.SubString2(0, 2)
Dim DateGPS3 As String = parts(4)
Dim YearGPS As String = DateGPS3.SubString2(0, 4)

even if your initial ReadData string is not empty, some of its parts()
could be. eg, DateGps could be empty. DateGPS.SubString2(0, 2)
would therefore throw an exception. you need to make sure that
DateGps.length > 0 before trying to use it.

also, you need to make sure your substring() operations are using
valid indexes. eg, Dim seconds As String = Time.SubString2(4, 6).
you may have miscalculated exactly where the seconds string is
within the Time string.
 
Upvote 0

Similar Threads

Top