Bug? B4J bug

mik1214324

Member
Licensed User
Hello Erel!
In B4J 8.10 I was able to initialize global b4Xview variables like this
test:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private lbl As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    MainForm.RootPane.AddNode(makelbl(lbl),0,0,100,100)
    lbl.Text="new text"
End Sub

Sub makelbl(bx As B4XView) As B4XView
    Dim l As Label
    l.Initialize("")
    bx=l
    l.Text="test"
    Return bx
End Sub

but starting from B4J 8.30 it throws error "java.lang.RuntimeException: Object should first be initialized (B4XView)." at lbl.Text="new text".
Is it a bug or I'm doing something wrong?
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello Erel!
This should read Hello y'all, as in to everybody in the forum.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private lbl As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim TmpLbl As Label
        TmpLbl.Initialize(Null)
    lbl = TmpLbl
    MainForm.RootPane.AddNode(makelbl(lbl),0,0,100,100)
    lbl.Text="new text"
End Sub

Sub makelbl(bx As B4XView) As B4XView
    Dim l As Label
    l.Initialize("")
    bx=l
    l.Text="test"
    Return bx
End Sub

Code not tested, but nevertheless should work as expected...

Enjoy...
 

mik1214324

Member
Licensed User
This should read Hello y'all, as in to everybody in the forum.

That's true, my apologize.

And of course I'm aware of your solution, but I want to know why this code works in 8.10 and dont works in 8.30+. Because as I understand if global b4xview variable passed to a sub then it should be passed like a pointer to the object. So no matter were it will be initialized, isnt it?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not a bug. It is actually a correction of a wrong behavior. You can read about it in the release notes.

Assigning a value to a variable, replaces the previous content of the variable. So when you call bx = l, it is not expected that the object referenced by bx will be modified. It is expected that the local bx variable will not point to the object referenced by l.

The code in this case should be:
B4X:
Sub makelbl As B4XView
    Dim l As Label
    l.Initialize("")
    l.Text="test"
    Return l
End Sub

Dim bx As B4XView = makelbl
 

mik1214324

Member
Licensed User
test:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim mp As Map
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    
    MainForm.Show
    test1(mp)
    Dim m2 As Map
    m2.Initialize
    m2.Put("test","one")
    mp.Put("m2",m2)
    Log(mp.Get("m2"))  'one
    test(mp)
    Log(mp.Get("m2"))  'two
End Sub

Sub test1(m As Map)
    m.Initialize
End Sub
Sub test(m As Map)
    Dim m2 As Map=m.Get("m2")
    m2.Put("test","two")
End Sub
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Please note that your test1 sub's logic is not like the makelbl sub's logic. If you change test1 to the same logic
B4X:
Sub test1(m As Map)
    Dim dummyMap As Map
    dummyMap.Initialize
    m = dummyMap
End Sub
You'll get the same error as in your first example. Here, similar to the first example you posted, a new map is created, initialized and then assigning to the global map. In your second example, you are directly initializing the global variable. Now, if you do this

B4X:
Sub test1(m As Map) As Map
    Dim dummyMap As Map
    dummyMap.Initialize
    m = dummyMap
    Return m
End Sub

And replace (in AppStart)
B4X:
test1(mp)
with
B4X:
mp = test1(mp)
Everything works.

then everything works as normal.
 

OliverA

Expert
Licensed User
Longtime User
More fun, no globals needed:

In AppStart (for demo purposes):
B4X:
    Dim myMap As Map
    myMap.Initialize
    myMap.Put("one", "try to overwrite me!")
    test6(myMap)
    Log(myMap)
with test6 code being
B4X:
Sub test6(m As Map)
    m.Initialize
    m.Put("two","I'm the onle one!!!!")
    Dim dummyMap As Map
    dummyMap.Initialize
    dummyMap.Put("Three", "You expect me, but will never see me! Guess why")
    m = dummyMap
End Sub
 
Top