java.lang.RuntimeException: Object should first be initialized (Label).

DouglasR

Member
Licensed User
Longtime User
The same code a few minutes earlier worked just fine. Now I get the error:
java.lang.RuntimeException: Object should first be initialized (Label).

There are NO controls in the layout.
Here is the offending code:

Sub Globals
DimlblSPOTS(81) AsLabel'Labels for the spots.
End Sub

Sub Activity_Create(FirstTime AsBoolean)
Activity.LoadLayout("mylayout")
Dim x AsInt
Dim y AsInt
For x = 1To10
For y = 0To70Step10
Dim l AsLabel
l.Initialize("lblSPOTS")
Activity.AddView(l,(((x*50)-50)),y*5,45,45)
Next
Next
End Sub

Sub lblSPOTS_Click
Dim c AsLabel
c = Sender
lblSPOTS(c.Tag).Color = Colors.green
End Sub

Any help would be appreciated. Thank you!
 

klaus

Expert
Licensed User
Longtime User
Different problems:
- You don't set the Tag property of the labels.
- You dont set the different labels to the array.

This code works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim x, y As Int
    
    For x = 0 To 9
        For y = 0 To 7
            Dim lbl As Label
            lbl.Initialize("lblSPOTS")
            Activity.AddView(lbl, x * 50, y * 50, 45, 45)
            lbl.Tag = y * 10 + x
            lbl.Color = Colors.Blue
        Next
    Next
End Sub

Sub lblSPOTS_Click
    Dim lbl As Label
    Dim x, y As Int
    
    lbl = Sender
    lbl.Color = Colors.green
    
    y = Floor(lbl.Tag / 10)
    x = lbl.Tag Mod 10
    Activity.Title = "Button x = " & x & "  y = " & y
End Sub
Best regards.
 

DouglasR

Member
Licensed User
Longtime User
Klaus,

Thank you for the fast response! Why can't I use the tag property? Thank you for the code, although it does not resolve my situation. How can I tell when the clicked label was already green and needs to be blue again? You can't 'if-then' the color.:sign0087:

Best Regards,
DouglasR
 

klaus

Expert
Licensed User
Longtime User
Why can't I use the tag property?
Who said that you couldn't ?

I told you that in your code you did't give the Tag property any value when you added the Labels.
In the code I posted I use the Tag property !
You got an answer to your question you didn't ask for a color change.
To solve your problem you can use a Type variable for the Tag property.
B4X:
Sub Process_Globals
    Type Tags(row As Int, col As Int, blue As Boolean)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim x, y As Int
    
    For x = 0 To 9
        For y = 0 To 7
            Dim lbl As Label
            Dim rcb As Tags
            rcb.row = y
            rcb.col = x
            rcb.blue = True
            lbl.Initialize("lblSPOTS")
            Activity.AddView(lbl, x * 50, y * 50, 45, 45)
            lbl.Tag = rcb
            lbl.Color = Colors.Blue
        Next
    Next
End Sub

Sub lblSPOTS_Click
    Dim lbl As Label
    Dim rcb As Tags
    
    lbl = Sender
    rcb = lbl.Tag
    
    If rcb.blue = True Then
        lbl.Color = Colors.Green
    Else
        lbl.Color = Colors.Blue
    End If
    rcb.blue = Not(rcb.blue)
    Activity.Title = "Button x = " & rcb.col & "  y = " & rcb.row
End Sub
Best regards.
 

DouglasR

Member
Licensed User
Longtime User
I just wanted to thank you for your help and patience. Your posts have completely fixed my issue. Please forgive me, I am often frustrated with having to memorize a bunch of non-BASIC commands and functions (which is why I gave up on trying to learn to use Eclipse in the first place). You've been very helpful.

Regards,
DouglasR
 
Last edited:
Top