B4J Question Variable size arrays

drponciano

Member
Licensed User
Longtime User
Am I missing something? I'm having trouble with an array of bytes. I declare variable x as Dim x(10000) as byte in Process Globals. When running the program I eventually get a ArrayIndexOutOfBoundsExeption error with an index in x well below the size, say x(i), where i=1113. If I log(x.length) I get different sizes during the process and, of course! when x.length is less than the index (say 1113) the error occurs. Is x(10000) of variable size? How can I get it fixed size?
 
Last edited:

drponciano

Member
Licensed User
Longtime User
Well, variable name is not x, I used x as an example. The variable is decalerd only once in Process Globals en used in a AsyncStreamDataAvailable.
 
Upvote 0

drponciano

Member
Licensed User
Longtime User
More correct to say that arrays can be redeclared with a new size.

Sounds to me that you should use BytesBuilder: https://www.b4x.com/android/forum/threads/89008/#content

I declare the variable only once in Process Globals and use it during AsyncStreamDataAvailable routine. I'm trying to copy part of the input buffer to this global array. The problem occurs randomly. I'll try to use BytesBuilder but, I think, shouldn't be neccesary.
 
Upvote 0

drponciano

Member
Licensed User
Longtime User
After a good night sleep things look clearer. My mistake. Sometimes, depending on the data flow, I copy part of the input buffer to x with a FOR loop but, some other times, I copy the whole buffer with x=inputbuffer and... thas was the error! That statement redefines x with same size as inputbuffer! Thanks for your help. Good morning, afternoon or night wherever you are.:)
 
Upvote 0
Top