Initializing variable of custome type

PenguinHero

Member
Licensed User
Longtime User
Hi All,

I've got a problem I can't work out.
I have created some nested types, and declared a variable of the final type.

I realised I had forgotten to initialise that variable when I got an NPE when I first tried to assign a value to one of the items in the variable.

I checked the forums and found Erel's example of using types to create a linked list (http://www.b4x.com/forum/basic4andr...linked-list-using-type-keyword.html#post39085), where he shows the initialize.

But, I have used arrays nested in the types, which seems to be stopping the initialize from working, as I still get an NPE when I assign a value.

I have copied the key code from my program. Can anyone tell me what needs to be done? Do I need to initialize the lower level items somehow?

B4X:
Sub Process_Globals 
  Type SingleUnit (UnitX As Int, UnitY As Int, UnitAlive As Boolean, UnitImage As ImageView)
  Type Squad (Unit(4) As SingleUnit)
  Type Army (Tank(2) As Squad, Cavalry(2) As Squad, Infantry(2) As Squad, Artillery As Squad, General As SingleUnit)
End Sub

Sub Globals
  Dim Player(2) As Army
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Player(0).Initialize
  Player(1).Initialize
  Player(0).Tank(0).Unit(0).UnitX = 1
End Sub

The line with the assignment of 1 to the UnitX gives an NPE.
I can't just do Player.Initialize as that gives a syntax error.

What am I doing wrong? Or can't I create types like this with the arrays embedded? Or is my usage of the UnitX item wrong?
I tried removing the array from the Player DIM and using the Player.Initialize but it still gives an NPE on the modified (no index on the Player) assignment.


Thanks,
PH
 

alhowiriny

Member
Licensed User
Longtime User
Hi All,
Do I need to initialize the lower level items somehow?

i think the answer is yes.. because this worked for me:

B4X:
Sub Process_Globals 
  Type SingleUnit (UnitX As Int, UnitY As Int, UnitAlive As Boolean, UnitImage As ImageView)
  Type Squad (Unit(4) As SingleUnit)
  Type Army (Tank(2) As Squad, Cavalry(2) As Squad, Infantry(2) As Squad, Artillery As Squad, General As SingleUnit)
End Sub

Sub Globals
  Dim Player(2) As Army
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Player(0).Initialize         'Initialize Army since it uses custome type
  Player(0).Tank(0).Initialize   'Initialize Squad since it uses custome type

  Player(0).Tank(0).Unit(0).UnitX = 1
End Sub
 
Upvote 0

PenguinHero

Member
Licensed User
Longtime User
Thanks alhowiriny,

That let me reference the Int values in the SingleUnit, but I found as soon as I tried to reference the UnitImage it failed with another NPE.
So I extended your reasoning and added:

Player(0).Tank(0).Unit(0).Initialize

And that let me initialise and assign a bitmap to the UnitImage.
Its now letting me draw them on a panel, so its looking good.

I'll just set up some nested FOR loops to do the full Initialisation in one big go.
 
Upvote 0

alhowiriny

Member
Licensed User
Longtime User
that's correct.. because your are using ImageView which is not a primitive type.

thank you for the feedback.
 
Upvote 0
Top