Android Question IndexOutOfBoundsException

mechanicaltesla

Member
Licensed User
Hi Everyone,

I'm using Nice Spinner with dependent Combo Boxes
As the combo boxes get populated I get the following error?
If I understand this correctly the index should equal two?


How would I set up the correct number of indexes?

java.lang.IndexOutOfBoundsException: Invalid index 5, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at main.java.org.angmarch.views.NiceSpinnerAdapter.getItemInDataset(NiceSpinnerAdapter.java:37)
at main.java.org.angmarch.views.NiceSpinner.setAdapterInternal(NiceSpinner.java:253)
at main.java.org.angmarch.views.NiceSpinner.attachDataSource(NiceSpinner.java:248)
at nicespinnerwrapper.niceSpinnerWrapper.attachDataSource(niceSpinnerWrapper.java:207)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)

B4X:
sub sizecmb_spinner_touched
    
    
    Log("sizecmb spinner touched")
    
    Dim l As List : l.Initialize
    Dim query1 As String
    l.Add("SELECT SIZE")
    For i=0 To l.Size -1
        
        l.Add(l.Get(i))
    
    Next


Log(l)
    

    
    
    If weightcmbval="SELECT WEIGHT" And connectionnamecmbval="SELECT CONNECTION" Then
        query1="SELECT distinct SIZE FROM DB order by SIZE asc"
    Else If weightcmbval <> "SELECT WEIGHT" And connectionnamecmbval="SELECT CONNECTION"  Then
        query1="SELECT distinct SIZE FROM DB WHERE WEIGHT=" & """" & weightcmbval &"""" &" order by SIZE asc"
    
    Else If weightcmbval <> "SELECT WEIGHT" And connectionnamecmbval <> "SELECT CONNECTION" Then
        query1="SELECT distinct SIZE FROM DB WHERE CONNECTIONNAME=" & """" & connectionnamecmbval  & """" & "AND WEIGHT=" & """" & weightcmbval &"""" &" order by SIZE asc"
    Else If weightcmbval="SELECT WEIGHT" And connectionnamecmbval <> "SELECT CONNECTION" Then
        query1="SELECT distinct SIZE FROM DB WHERE CONNECTIONNAME=" & """" & connectionnamecmbval & """" & " order by SIZE asc"

    Else
        query1="SELECT distinct SIZE FROM DB order by SIZE asc"
    
    End If
    Log(query1)
    Dim rs As ResultSet = sql1.ExecQuery(query1)

    Do While rs.NextRow
        l.Add(NumberFormat2(rs.GetString("SIZE"),0,3,3,False))
    Loop
    Log(l)
    
    sizecmb.attachDataSource(l)

    rs.Close
    
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
java.lang.IndexOutOfBoundsException: Invalid index 5, size is 2

You are using index 5 but the list/Array only has 2 items. Not 6.

We can not say more as you are not providing enough informations/you posted incomplete code.

Upload a small project which shows the issue.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
We can not say more as you are not providing enough informations/you posted incomplete code.
Well, we could say something about this code:
B4X:
1  Dim l As List : l.Initialize
2  Dim query1 As String
3  l.Add("SELECT SIZE")
4  For i=0 To l.Size -1
5      l.Add(l.Get(i))
6  Next
Line 1 creates an empty list. No complaint so far.
Line 2 we can ignore, other than wondering why it is located here, and not closer to where it is actually used.
Line 3 adds "SELECT SIZE" to the list, thus the list is now 1 item long.
Line 4 translates to "For i= 0 to 0", which will do one loop through the next line.
Line 5 translates to "l.Add(l.Get(0))", which will add another copy of the first and only item currently in the list ("SELECT SIZE") thus the list is now 2 items long (and both are "SELECT SIZE"... to be sure, to be sure?)
Line 6 should terminate the loop, because in line 4 we said to only do the loop once (from 0 to 0)

Even if B4A used the revised loop upper limit, which changed from 0 to 1 when we added the second item to the list, that would mean the loop would then run to infinity (but not beyond!) because each time we added another item to the list, the terminating value of the loop would be incremented just out of our reach again. I'm guessing this is not happening.

So, at the point where you log(l) you should be seeing two items in the list, and in the bit where you are accessing l.Get(5) apparently there are still only two items in the list.

I can't actually see where the l.Get(5) is occurring in your supplied code, but it's late here and I'm tired so presumably my brain is starting to shut down for the day.

:)
 
Last edited:
Upvote 0

mechanicaltesla

Member
Licensed User
No infinite loop the combo boxes do
java.lang.IndexOutOfBoundsException: Invalid index 5, size is 2

You are using index 5 but the list/Array only has 2 items. Not 6.

We can not say more as you are not providing enough informations/you posted incomplete code.

Upload a small project which shows the issue.

DonManfred, emexes,

No infinite loop the combo boxes do filter but it goes from small to big list it crashes.

Please see link to download the example.

Please note I’m using nice spinner

https://drive.google.com/open?id=1TXiLu56IQANTB6pbw8qJi2SNaRRE9qdW
 
Upvote 0
Top