Android Question Array error

tdocs2

Well-Known Member
Licensed User
Longtime User
GREETINGS!

THANK YOU FOR ANSWERING THIS REALLY NEWBIE QUESTION!

I feel I have walked into the twilight zone...

B4X:
Dim a1() As String
    For i = 0 To 5
        a1(i)="T"&i
    Next

ERROR:
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

"T"&i are used for illustration purposes and represent variables coming from a data base.

This is not a case of loading constants into an array like:

B4X:
Dim a1() As String = Array As String("Ian", "Mark", ...)

Any help will be welcomed.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
You are creating an empty array.

You should set the size when you declare the array (assuming that you are not assigning a different array like in the second code snippet):
B4X:
Dim a1(6) As String
For i = 0 To 5  '5 could be any number dependent on the data base input
...

Thank you, Erel.

I wanted a dynamic array. I do not know how many values are coming in from the data base. The number 5 could be n...

I know this works:

B4X:
    Dim list1 As List
    list1.Initialize
    For i = 0 To 5
        list1.Add("T"&i)
    Next

Is the list the correct control to be used for dynamic arrays?

Thanks again.

Sandy
 
Last edited:
Upvote 0
Top