Android Question for next loop, set value of an array

Colin Evans

Active Member
Licensed User
Longtime User
Hi, could some one please explain why the program stops with a 'java.lang.ArrayIndexOutOfBoundsException: length=0; index=0' on the line containing N(i) = tim0, and if that line is rem'd out the next line V(i) = cons, I need to get the variables to put in a Bar chart any idea's would be gratefully accepted, many thanks

Both of them are defined in the Process_Globals as

B4X:
    Public N() As String
    Public V() As Double

B4X:
For i = 0 To 99
                Dim str As Map = results.Get(i)
                Dim strVar As String = str.Get("interval_end")
                Dim strArray() As String=Regex.Split("T",strVar)
                Dim TextExtract As String=strArray(0)
                from=TextExtract
                cons = str.Get("consumption")
                tim0 = str.Get("interval_start")
                Dim strVar As String=tim0
                Dim strArray() As String=Regex.Split("T",strVar)
                Dim TextExtract As String=strArray(1)
                Dim strArray() As String=Regex.Split("Z",TextExtract)
                Dim TextExtract As String=strArray(0)
                Dim strArray() As String=Regex.Split(":",TextExtract)
                Dim TextExtract As String=strArray(0) & ":" & strArray(1)
                tim0=TextExtract
                tim1 = str.Get("interval_end")
                Dim strVar As String=tim1
                Dim strArray() As String=Regex.Split("T",strVar)
                Dim TextExtract As String=strArray(1)
                Dim strArray() As String=Regex.Split("Z",TextExtract)
                Dim TextExtract As String=strArray(0)
                Dim strArray() As String=Regex.Split(":",TextExtract)
                Dim TextExtract As String=strArray(0) & ":" & strArray(1)
                tim1=TextExtract
                N(i) = tim0
                V(i) = cons
                AddRow(Array As String(from, tim0, tim1, cons))
            Next
 

drgottjr

Expert
Licensed User
Longtime User
you either have to know how many elements n() holds, or you need to use a list
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi thanks for the reply, not sure I understand the reply, there are 99 entries of tim0 and cons so the array contains details for each of the 99 N(i) and V(i), sorry beginner trying to learn
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Hi thanks for the reply, not sure I understand the reply, there are 99 entries of tim0 and cons so the array contains details for each of the 99 N(i) and V(i), sorry beginner trying to learn
But you did not declare arrays with the size of 99 elements. You declared arrays with size of 0. If you know up front that you need arrays of that size just do
B4X:
    Public N(100) As String
    Public V(100) As Double
Now why did I use 100? Because of your for next declaration:
B4X:
For i = 0 To 99
This will iterate 100 times (if left alone - no EXIT used).
Why did you not have to use a size when you did:
B4X:
Dim strArray() As String=Regex.Split(":",TextExtract)
Because, technically Dim strArray() As String creates a reference to an empty array. Regex.Split returns an array reference, assigning it to strArray. Since strArray is an array reference, that assignment works.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
BTW, is this what you are trying to achieve with the splitting and concatenation of strings?
B4X:
    Dim tim0 As String = "2020-05-06T21:30:00Z"
    tim0 = tim0.SubString2(11,16)
    Log(tim0)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thank you for your help so far but I've got one more problem that I don't understand, the variables
Public N(100) As String
Public V(100) As Double are defined in Process_Globals
and now as part of the redefined code get allocated the correct data
B4X:
For i = 0 To 99
                    Dim str As Map = results.Get(i)
                    Dim strVar As String = str.Get("interval_end")
                    Dim strArray() As String=Regex.Split("T",strVar)
                    Dim TextExtract As String=strArray(0)
                    from=TextExtract
                    cons = str.Get("consumption")
                    tim0 = str.Get("interval_start")
                    tim0 = tim0.SubString2(11,16)
                    tim1 = str.Get("interval_end")
                    tim1 = tim1.SubString2(11,16)
                    N(i) = tim0 
                    V(i) = cons
                    AddRow(Array As String(from, tim0, tim1, cons))
                Next

But when I try to add the values to a BarChart all I get are zero's, I'm obviously missing the logic somewhere but I thought the globals defined would be accessible to any Private Sub

B4X:
Private Sub FillCharts
    Dim i As Int
    Name = Array As String(N(1), N(2), N(3),N(4),N(5),N(6),N(7),N(8),N(9),N(10),N(11),N(12),N(13),N(14),N(15),N(16),N(17),N(18),N(19),N(20),N(21),N(22),N(23),N(24))
    Value = Array As Double(V(1), V(2), V(3),V(4),V(5),V(6),V(7),V(8),V(9),V(10),V(11),V(12),V(13),V(14),V(15),V(16),V(17),V(18),V(19),V(20),V(21),V(22),V(23),V(24))
    BarChart.AddBar("Consumption", xui.Color_RGB(139, 132, 255))
    For i = 0 To Name.Length - 1
        BarChart.AddBarPointData(Name(i), Value(i))
    Next
    BarChart.DrawChart
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Make it another question and we'll go from there. Freebie hint:
both N and V are already arrays, so
B4X:
   Name = Array As String(N(1), N(2), N(3),N(4),N(5),N(6),N(7),N(8),N(9),N(10),N(11),N(12),N(13),N(14),N(15),N(16),N(17),N(18),N(19),N(20),N(21),N(22),N(23),N(24))
    Value = Array As Double(V(1), V(2), V(3),V(4),V(5),V(6),V(7),V(8),V(9),V(10),V(11),V(12),V(13),V(14),V(15),V(16),V(17),V(18),V(19),V(20),V(21),V(22),V(23),V(24))
can just be
B4X:
Name = N
Value = V
unless the goal is to create duplicate arrays. This way you just change the name you use to reference them. If you really want to cut down on the coding:
B4X:
Private Sub FillCharts
    BarChart.AddBar("Consumption", xui.Color_RGB(139, 132, 255))
    For i = 0 To Name.Length - 1
        BarChart.AddBarPointData(N(i), V(i))
    Next
    BarChart.DrawChart
End Sub
You can even take out the declaration for i, since the For loop will do that for you (it will default to an Int declaration). As to your question, create a new post. Show how you fill V with data and then show this sub and we'll go from there.
 
Upvote 0
Top