Cannot initialize if held in View?

PaulR

Active Member
Licensed User
Longtime User
Hi

I am declaring a couple of custom Type objects to clean up my code, but have hit an issue with attempting to Initialize them directly. Here is some example code showing the issue I am encountering in my app....
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Type Ob(bt As Button)
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim b As Ob
   Dim butt As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
'   b.bt.Initialize("")                  'fails here
'   Activity.AddView(b.bt, 0, 0, 200, 100)

'   Dim middleMan As Button
'   middleMan = b.bt                  'fails here
'   middleMan.Initialize("")
'   Activity.AddView(middleMan, 0, 0, 200, 100)

'   butt.Initialize("")                  'normal way works
'   Activity.AddView(butt, 0, 0, 200, 100)

   b.bt = InitButton                  'works
   Activity.AddView(b.bt, 0, 0, 200, 100)
   
End Sub

Sub InitButton() As Button
   Dim bbb As Button
   bbb.Initialize("")
   Return bbb
End Sub
.... autocomplete shows Initialize as an option for b.bt or middleMan in that code, but the app hangs due to a nullPointerException.

I know a few 'Init' Subs in a Code Module can solve this, but it smells like we should be able to do it directly.
 

Ricky D

Well-Known Member
Licensed User
Longtime User
you should only do

b.Initialize

You need to initialize the Type but not it's members

cheers, Ricky
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
Thanks Ricky, that was very nearly it. :)

B4X:
b.Initialize
   b.bt.Initialize("")
   Activity.AddView(b.bt, 0, 0, 200, 100)
.... compiles and doesn't crash, not without the first or second line though.

At least I didn't have to do anything silly like....
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub iButton (name As String) As Button
   Dim b As Button
   b.Initialize(name)
   Return b
End Sub

Sub iPanel (name As String) As Panel
   Dim p As Panel
   p.Initialize(name)
   Return p
End Sub

Sub iCanvas (p As Panel) As Canvas
   Dim c As Canvas
   c.Initialize(p)
   Return c
End Sub

Sub iGestures(p As Panel, name As String) As Gestures'For SetOnTouchListener
   Dim g As Gestures
   g.SetOnTouchListener(p, name)
   Return g
End Sub
   
Sub iMap() As Map
   Dim m As Map
   m.Initialize
   Return m
End Sub

Sub iRect(Left As Int, Top As Int, Right As Int, Bottom As Int) As Rect
   Dim r As Rect
   r.Initialize(Left, Top, Right, Bottom)
   Return r
End Sub

Sub iPointArray(size As Int) As Point()
   Dim arr(size) As Point
   For i = 0 To size - 1
      arr(i).Initialize
   Next
   Return arr
End Sub
:D

Paul
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
ok, so what is the final code?

I'm not sure what you mean by "very nearly it" ?

regards, Ricky
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
The final code was that I needed to initialize the Type first then the object held in it.....
B4X:
   b.Initialize 'initialize this Type

    b.bt.Initialize("") 'then this works
    Activity.AddView(b.bt, 0, 0, 200, 100)
.... thanks again. :)
 
Upvote 0
Top