Android Question Indexoutofboundsexception help me pls

SalentoFC

Member
Licensed User
Longtime User
Hi

I'd like to know if you can help me. It's about a week I'm working about, but I can't find the error.

In the module Levels, in the Sub "Estrazione2Numeri", I think there's an error about line 158.

This should be a memory game. The first level is ok. The array of 6 buttons will be created, but, when the buttons are deleted for going to the second level (array of 12 buttons) when the code go to Sub "InserisciRigaLista" and the variable TotEstratti go to 12 (TotEstratti(10) + 2), the code return to the Sub "Estrazione2Numeri".. there something crash and the system put in the log "IndexOutOfBoundException", without indicate the line error (it's an error of program that's can't resolving into the code.

But why? It's impossible. I've controlled this many and many times, but I don't know what be the problem.

Thanks very much at all.

You can download the complete project here: http:\\robertodaniele.altervista.org\BicentenarioArmaCC.zip

Roberto.
 

sorex

Expert
Licensed User
Longtime User
an array of 12 means from 0 to 11, so your code is wrong.

it should be


(TotEstratti(10-1) + 2)
 
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
Now I
H

-------------------------------------

Has you tested it with the code on the line project? Because the code has many parts where there should be an eventual error. I'm waiting for your precious reply. Tks

Now I'm watching about the code. The word "TotEstratti" and Last "(10)" is to indicate the value but is not code. You can considerate it as a comment for you. The right code is: (TotEstratti + 2)
While TotEstratti = 10 the code working ok but when it begin 12 and return to the called code the project crash.
And for this, I've indicate the link to download the project in all and prove it.
TotEstratti isn't an array but an int variable.
Tks again

P.S. Who will be able to resolv it I will offer him/Her an holiday in my house to Italy.. ha ha ha .. ;-)
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, I didn't check the actual code but it was just based on your comments.

will have a look at the file now...
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the problem is in this block

B4X:
Do While (TotEstratti < maxnumber) AND (entratomatrice = False)
Dim numero1 As Int = Rnd(1,maxn)
Dim numero2 As Int = Rnd(1,maxn)
If numero1 <> numero2 Then
ControllaEstratti
Else
EstraiNumeri
End If
Loop

can you explain what ControllaEstratti & EstraiNumeri is doing?
 
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
the problem is in this block

B4X:
Do While (TotEstratti < maxnumber) AND (entratomatrice = False)
Dim numero1 As Int = Rnd(1,maxn)
Dim numero2 As Int = Rnd(1,maxn)
If numero1 <> numero2 Then
ControllaEstratti
Else
EstraiNumeri
End If
Loop

can you explain what ControllaEstratti & EstraiNumeri is doing?

controllaestratti verify if the 2 numbers are different (the numbers is the position of the buttons in the design)
EstraiNumeri permite to find the numbers but there go directly to the Sub "Estrazione2Numeri", so here.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you seem to create endless loops by calling subs that contain a call to the current sub.
 
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
"sorex, post: 273381, member: 25700"]you seem to create endless loops by calling subs that contain a call to the current sub.[/quote]

Believe me.. is about 2 weeks I don't able to resolv it.. Too I've notate about the endless loops..
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
i'm affraid the logic is way of of what it should've been so it's written in a too complex way. :(
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
here's how it could be done.

it contains a level based cardset generator & shuffle routine which is what your game needs.

then you can see the shuffled result in the log window.

B4X:
Sub Process_Globals
Dim level As Int
Dim levels() As Int
levels=Array As Int(0,6,12,16)

Dim cardset() As Int
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    level=2
    createset
    shuffle
End Sub


Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub createset()
Dim r As Int
Dim cardset( levels( level ) ) As Int
For x=0 To levels(level)-1 Step 2
    cardset(x)=x/2
    cardset(x+1)=x/2
Next
End Sub

Sub shuffle() As String
Dim r As Int
Dim t As String
For x=0 To cardset.Length-1
    r=Rnd(0,cardset.Length)
    t=cardset(r)
    cardset(r)=cardset(x)
    cardset(x)=t
Next
'--- only for log reasons
 t=""
 For x=0 To cardset.Length-1
 t=t & cardset(x)
 Next
 Log(t)
'--- only for log reasons
End Sub
 
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
here's how it could be done.

it contains a level based cardset generator & shuffle routine which is what your game needs.

then you can see the shuffled result in the log window.

B4X:
Sub Process_Globals
Dim level As Int
Dim levels() As Int
levels=Array As Int(0,6,12,16)

Dim cardset() As Int
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    level=2
    createset
    shuffle
End Sub


Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub createset()
Dim r As Int
Dim cardset( levels( level ) ) As Int
For x=0 To levels(level)-1 Step 2
    cardset(x)=x/2
    cardset(x+1)=x/2
Next
End Sub

Sub shuffle() As String
Dim r As Int
Dim t As String
For x=0 To cardset.Length-1
    r=Rnd(0,cardset.Length)
    t=cardset(r)
    cardset(r)=cardset(x)
    cardset(x)=t
Next
'--- only for log reasons
t=""
For x=0 To cardset.Length-1
t=t & cardset(x)
Next
Log(t)
'--- only for log reasons
End Sub

Then, what I should modificate in my project? All the code is based on another different logic. Could you Say me where I must inserti this code? And why my code so wrote generate an endless loops without a reason? Tks
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
figuring out your weird code would probably take longer than implementing something that works right.

the code you have now builds an array, all you need is to loop through it to generate the views with the assiciated bitmaps.

so the base to start with it already correct. all you need to do is increase the level variable when a level is complete and call the generator/shuffle/cardview build again.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
here's a modded version with the routines integrated.

don't jump for joy yet because you need to check the click events as it still goes wrong there (don't have time to fix it all while I'm still at work)
 

Attachments

  • game_problem.zip
    10.8 KB · Views: 156
  • Like
Reactions: eps
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
here's a modded version with the routines integrated.

don't jump for joy yet because you need to check the click events as it still goes wrong there (don't have time to fix it all while I'm still at work)

Ok Tks very much
 
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
Thank you sorex: you are a great person! You have resolved the mystery.. ahahhah it's very good your work, it's a very great work!
P.S. You are officially invited to my house in Italy when you like! ;-p
Tks very much again! You are number 1!!! If I can do a donate for you, I should be very great!
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I'm glad you were able to continue on it.

You see that some things can be done easier when starting from the right track :)

If you're stuck again just post inhere again or visit the chat.

If I can do a donate for you, I should be very great!

that would be great, a link is in my signature :)
 
Upvote 0

SalentoFC

Member
Licensed User
Longtime User
I'm glad you were able to continue on it.

You see that some things can be done easier when starting from the right track :)

If you're stuck again just post inhere again or visit the chat.



that would be great, a link is in my signature :)

Yes, it's really True you Say.
P.S. Pls conferm me if you recept donation just sent you ok? TKS again for your help and sorry for my lacking english.. ;-p
 
Upvote 0
Top