1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question IndexOutOfBoundsException

Discussion in 'Android Questions' started by mechanicaltesla, Jun 24, 2019.

  1. mechanicaltesla

    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?

    Code:
    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
     
  2. DonManfred

    DonManfred Expert Licensed 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: Jun 24, 2019
  3. emexes

    emexes Well-Known Member Licensed User

    Well, we could say something about this code:
    Code:
    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: Jun 24, 2019
    José J. Aguilar likes this.
  4. mechanicaltesla

    mechanicaltesla Member Licensed User

    No infinite loop the combo boxes do
    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
     
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