B4J Question Multi-level types - unable to initialize - SOLVED

floatingpoint

Member
Licensed User
Longtime User
I would like to have a multi-level type structure similar to:

B4X:
Sub Process_Globals
   Type type1 (name As String, number As Int)
   Type type2 (org As String, assoc As Int, person(100) As type1)
   Type type3 (country As String, zip As Int, assoc(100) As type2)
   
   Dim individual As type1
   Dim company As type2
   Dim nationality As type3
End Sub

Then the following works:
B4X:
Public Sub Show
   If frm.IsInitialized = False Then
       frm.Initialize("frm", 440, 520)
       frm.RootPane.LoadLayout("Login")

       company.Initialize
       company.person(0).name = "ted"
       company.person(0).number = 5
       Log (company.person(0).name)

   End If
   frm.Show
End Sub

However the following compiles but results in a NullPointerException when run:
B4X:
Public Sub Show
   If frm.IsInitialized = False Then
       frm.Initialize("frm", 440, 520)
       frm.RootPane.LoadLayout("Login")

       nationality.Initialize
       nationality.assoc(0).person(0).name = "ted"
       nationality.assoc(0).person(0).number = 5
       Log (nationality.assoc(0).person(0).name)

   End If
   frm.Show
End Sub

How best to resolve this?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You might be best served by creating a factory method like this:
B4X:
Sub NewType3 As Type3
   Dim t3 As Type3
   t3.Initialize
   For Each t2 As Type2 in t3.assoc
      t2.Initialize
      For Each t1 As Type1 in t2.person
         t1.Initialize
      Next
   Next
   Return t3
End Sub

Then you would use it like so:
B4X:
Dim nationality As Type3 = NewType3
nationality.assoc(0).person(0).name = "ted"
nationality.assoc(0).person(0).number = 5
Log(nationality.assoc(0).person(0).name)

Beware, none of this is tested. Also, do you really need an array of 100 elements in Type2 and Type3? Initializing all of that will require 10,000 iterations. You might think about using a List instead and only adding elements as necessary.
 
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
You might be best served by creating a factory method like this:
...
Beware, none of this is tested.
Also, do you really need an array of 100 elements in Type2 and Type3?
Initializing all of that will require 10,000 iterations.
You might think about using a List instead and only adding elements as necessary.

Thank you, tested and works nicely :)
My mistake was to not initialize all instances of types 1 or 2!

My example is not my actual requirement which is 4 lots of 128 of 6 elements.
So not quite as big.

I Looked into lists, however I understand that multi-dimensioned lists are not supported.

PS. How do I change the title of this thread to 'Solved'?
- Found it - "Thread Tools" top right of page.

FP
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
What do you mean that multi-dimensional Lists are not supported? Each element in a List can be anything, including another List.
 
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
Last edited:
Upvote 0
Top