Android Question java.lang.ArrayIndexOutOfBoundsException at java.lang.reflect.Array.get

phukol

Active Member
Licensed User
Longtime User
hi guys!

Im processing a lot of data (around 7000+ arrays produced using split). During processing, im ecountering this error:
B4X:
Error occurred on line: 444 (Main_Page)
java.lang.ArrayIndexOutOfBoundsException: length=7; index=15
    at java.lang.reflect.Array.get(Array.java:72)
    at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
    at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:560)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:371)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA$2.run(BA.java:328)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

Here is my code:
B4X:
Dim upInfo(10000) As String = Regex.Split("`",strInfo)
    Dim intRec As String
    Dim intSQLExec As Int
    
            intSQLExec = 0
            For intA = 0 To upInfo.Length - 1
                If upInfo(intA).Length > 0 Then   
                    Dim myInfo(500) As String = Regex.Split("~",upInfo(intA))
                   
                    intRec = Starter.s.ExecQuerySingleResult("select exm_id from tbl_exam where exm_id = " & myInfo(15) )
                    'Starter.c = Starter.s.ExecQuery("select exm_id from tbl_exam where exm_id = " & myInfo(15) )
                    If intRec = Null Then 
                        intSQLExec = intSQLExec + 1
                         
                        Starter.s.AddNonQueryToBatch("insert into tbl_exam (exp_id,sbj_id,mode_,info_,picture_,answer_,a_,pic_a,b_,pic_b,c_,pic_c,d_,pic_d,e_,pic_e,rationale_,exm_id,exm_free ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", _
                        Array As Object(myInfo(16),myInfo(18),myInfo(0),myInfo(1),myInfo(2),myInfo(3),myInfo(4),myInfo(5),myInfo(6),myInfo(7),myInfo(8),myInfo(9),myInfo(10),myInfo(11),myInfo(12),myInfo(13),myInfo(14),myInfo(15),myInfo(20)   )) 
                   
                        If intSQLExec > 100 Then
                            intSQLExec = 0
                            Starter.s.ExecNonQueryBatch("SQL")
                        End If
                    End If
                   
                End If
            Next
            If intSQLExec > 0 Then
                intSQLExec = 0
                Starter.s.ExecNonQueryBatch("SQL")
            End If

Notice that i tried calling ExecNonQueryBatch every 100th times just to process the data immediately. Note that even i i remove this, the error still persists. What could be the reason
 

LucaMs

Expert
Licensed User
Longtime User
IndexOutOfBounds

I think that Regex "redims" your array:
Dim myInfo(500) As String = Regex.Split("~",upInfo(intA))

and one of your upInfo is made of only 7 words, so you get the error in:
intRec = Starter.s.ExecQuerySingleResult("select exm_id from tbl_exam where exm_id = " & myInfo(15) )


To find the error, you could write:
B4X:
Dim myInfo() As String = Regex.Split("~",upInfo(intA))
If myInfo.Length < 16 Then
    log(intA & Tab & upInfo(intA))
End If
 
Upvote 0
Top