List Initialize -> NullPointerException??

JogiDroid

Member
Licensed User
Longtime User
This is weird...

B4X:
Sub Process_Globals
   Type UserDetails(username As String, password As String, loginOk As Boolean, firstname As String, lastname As String, email As String, players As List, courses As List)

   Dim user As UserDetails
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
      user.courses.Initialize ' init  list
      user.players.Initialize ' init  list
   End If   
End Sub

spits out just "[B]java.lang.NullPointerException[/B]" at "user.courses.Initialize"

Bug or just stupid beginner mistake ? :)
 

JogiDroid

Member
Licensed User
Longtime User
Good point, strangely 'user' was working quite right before I added those 2 lists.. But still, I was assuming types don't need to initialize, -> worked for while but I really should read docs better!! Thanks!
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I have found that if anything in the Typedef needs initializing, I need to initialize the Typedef first.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Good point, strangely 'user' was working quite right before I added those 2 lists
Not strange if you think about it. If your type only contained primitive values then they are in effect initialised to a default value when you dim the instance of the type as dimming the type allocates the space for their values. However Lists are true objects, called reference types, and dimming the type allocates space for what is a pointer to the instance of the type. Those pointers however need initialising to point at a new instance of the type which is why you need to call Initialize. The same applies if your type contains arrays.
 
Upvote 0
Top