Invalid index 35, size is 35?

PharCyDeD

Active Member
Licensed User
Longtime User
Every now and then I get this error java.lang.IndexOutOfBoundsException: Invalid index 35, size is 35...and I am unsure of why. I generate 36 random numbers and then assign them to a listview...then pull them out of the listview like:

B4X:
Number1 = ListView1.GetItem(0)
Number2 = ListView1.GetItem(1)
Number3 = ListView1.GetItem(2)
...
...
Number36 = ListView1.GetItem(35)

Most of the time everything goes fine, but sometimes this stops the program and highlights 'TheNumberIs = ListView1.GetItem(35)'

My program calls this process many times and usually does fine. Any idea of what is going wrong here? How can it be out of bounds if the size is 0-35?
 

PharCyDeD

Active Member
Licensed User
Longtime User
I have this:

(LocationGenerator is a map)

B4X:
Sub GenerateLocations


   LocationGenerator.Put(Rnd(1, 37), "") 'generate random number between 1 and 36
   LocationGenerator.Put(Rnd(1, 37), "")
   etc..
   
   
   
   
   For i = 0 To LocationGenerator.Size - 1
      ListView1.AddSingleLine(LocationGenerator.GetKeyAt(i))
   Next
   
   SetLocations

End Sub


Then I get the error in:

B4X:
Sub SetLocations

   Spot1Location = ListView1.GetItem(0)
   SetSpot1Location
   Spot1MatchLocation = ListView1.GetItem(1)
   SetSpot1MatchLocation
   Spot2Location = ListView1.GetItem(2)
   SetSpot2Location
   Spot2MatchLocation = ListView1.GetItem(3)
   SetSpot2MatchLocation
   Spot3Location = ListView1.GetItem(4)
   SetSpot3Location
   Spot3MatchLocation = ListView1.GetItem(5)
   SetSpot3MatchLocation
   Spot5Location = ListView1.GetItem(6)
   SetSpot5Location
   Spot5MatchLocation = ListView1.GetItem(7)
   SetSpot5MatchLocation
   Spot18Location = ListView1.GetItem(8)
   SetSpot18Location
   Spot18MatchLocation = ListView1.GetItem(9)
   SetSpot18MatchLocation
   Spot17Location = ListView1.GetItem(10)
   SetSpot17Location
   Spot17MatchLocation = ListView1.GetItem(11)
   SetSpot17MatchLocation
   Spot13Location = ListView1.GetItem(12)
   SetSpot13Location
   Spot13MatchLocation = ListView1.GetItem(13)
   SetSpot13MatchLocation
   Spot4Location = ListView1.GetItem(14)
   SetSpot4Location
   Spot4MatchLocation = ListView1.GetItem(15)
   SetSpot4MatchLocation
   Spot7Location = ListView1.GetItem(16)
   SetSpot7Location
   Spot7MatchLocation = ListView1.GetItem(17)
   SetSpot7MatchLocation
   Spot6Location = ListView1.GetItem(18)
   SetSpot6Location
   Spot6MatchLocation = ListView1.GetItem(19)
   SetSpot6MatchLocation
   Spot8Location = ListView1.GetItem(20)
   SetSpot8Location
   Spot8MatchLocation = ListView1.GetItem(21)
   SetSpot8MatchLocation
   Spot9Location = ListView1.GetItem(22)
   SetSpot9Location
   Spot9MatchLocation = ListView1.GetItem(23)
   SetSpot9MatchLocation
   Spot14Location = ListView1.GetItem(24)
   SetSpot14Location
   Spot14MatchLocation = ListView1.GetItem(25)
   SetSpot14MatchLocation
   Spot15Location = ListView1.GetItem(26)
   SetSpot15Location
   Spot15MatchLocation = ListView1.GetItem(27)
   SetSpot15MatchLocation
   Spot12Location = ListView1.GetItem(28)
   SetSpot12Location
   Spot12MatchLocation = ListView1.GetItem(29)
   SetSpot12MatchLocation
   Spot11Location = ListView1.GetItem(30)
   SetSpot11Location
   Spot11MatchLocation = ListView1.GetItem(31)
   SetSpot11MatchLocation
   Spot16Location = ListView1.GetItem(32)
   SetSpot16Location
   Spot16MatchLocation = ListView1.GetItem(33)
   SetSpot16MatchLocation
   Spot10Location = ListView1.GetItem(34)
   SetSpot10Location
   Spot10MatchLocation = ListView1.GetItem(35)
   SetSpot10MatchLocation

End Sub
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
B4X:
LocationGenerator.Put(Rnd(1, 37), "") 'generate random number between 1 and 36
LocationGenerator.Put(Rnd(1, 37), "")
etc..
Why you don't just use a loop for generating those numbers? I bet the error is in that etc.. part. Are you sure there are 36 lines? Try this:
B4X:
For n = 0 To 35 ' Or try a bigger number like 100 to see if it gives the same error
    LocationGenerator.Put(Rnd(1, 37), "")
Next
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Actually I have to do:

LocationGenerator.Put(Rnd(1, 37), "")

a lot more than 36 times because for some reason it doesn't generate correctly to the map unless it is overdone. If you try this in a small test application you will see that it will only add a full list of random numbers 1-36 if you overdo the LocationGenerator.Put(Rnd(1, 37), "") line. Trying your code didn't output correctly for my app...but with tweaking (increasing the size of the loop) I got it working but that will most likely end up with the same error?



-----------
Edit: yeah same error tho I like that the code is much smaller in size :(
 
Last edited:
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I guess it may be worth saying that it ALWAYS generates the first level properly and I think it does the second one as well, but somewhere down the line it just crashes it :(
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
I did some testing in the app. I added a label for Map.size and a label for ListView.size. I made a button that calls the event to randomize, etc. The labels both read 36 through a random amount of clicks then suddenly both drop to 35. I put together a test project showing what happens. Just open it and click the "Generate Button" until the error pops up.
 

Attachments

  • TestProject1.zip
    6.9 KB · Views: 183
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The Map will only allow unique keys, so if you are using a random number generator you can guarantee that there are some duplicates that don't create an entry.

To ensure there are 36 entries you need to use a loop and either leave it to chance on how long it takes, or manipulate the data somewhat so that it only takes 36 tries.



B4X:
Do While LocationGenerator.Size < 36

     LocationGenerator.Put(Rnd(1, 37), "")

Loop

That could take ages to fill it on a bad day.

Or you could do something like this which will fill the next available slot to the random number and take 36 iterations.:

B4X:
Do while LocationGenerator.Size < 36

    Num=Rnd(1, 37)

   Result=""
   Do While Result = ""
       If LocationGenerator.GetDefault(Num,"NotFound") = "NotFound" Then
            LocationGenerator.Put(Num, "")
       Else
         Num=Num+1
         'If the last number is filled send it back to the beginning
         If Num = 37 Then Num = 0
      End If
   Loop
Loop
 
Last edited:
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Yeah, I was counting on the unique key entry of the Map to pull a set of random numbers between 1-36 without any duplicates. I will test your posted code out. What should I Dim Result as?
 
Upvote 0

PharCyDeD

Active Member
Licensed User
Longtime User
Actually:

B4X:
Do While LocationGenerator.Size < 36

     LocationGenerator.Put(Rnd(1, 37), "")

Loop

Completely solved the problem and doesn't cause any issues as far as I can see. Man you need to have a donate button under your name. You are an asset to this forum.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No Problem, result should be a String
 
Upvote 0
Top