Create sth.

BlitzMax Forums/BlitzMax Beginners Area/Create sth.

regaa(Posted 2004) [#1]
Strict

Type Ball
Field x
Field y
Field bl:TList

Function Create:Ball(_x:Int, _y:Int)
Local b:Ball

b=New Ball
b.x=_x
b.y=_y
If b.bl=Null Then b.bl=New TList
Return(b)
End Function

Function ShowBalls()
Local b:Ball=New Ball

For b:Ball=EachIn b.bl
Print "lol"
Next
End Function
End Type

Local b:Ball=Ball.Create(50,50)
b.ShowBalls()
WaitKey()

I just want to create an object called Ball, if i call ShowBalls, i get this Error:

Unhandled Exception: Attempt to access field or method of Null object
ShowBalls [$BMXPATH/regaa/temp/temp.bmx;21;3]

I know where the mistake is, but dont know how to solv this without global variables :(.


regaa(Posted 2004) [#2]
ok, here s the code i've needed:

THX to Dreamora:

Strict

Type Ball
Global bl:TList
Field x
Field y

Function Create:Ball(_x:Int, _y:Int)
  Local b:Ball

  b=New Ball
  b.x=_x
  b.y=_y
  If bl=Null Then bl=New TList
  bl.addlast( b )
  Return b
End Function

Function ShowBalls()
  Local b:Ball

  For b:Ball=EachIn Ball.bl
  Print "lol"
  Next
End Function
End Type

Local b:Ball=Ball.Create(50,50)
b.ShowBalls()
WaitKey()


LeisureSuitLurie(Posted 2004) [#3]
Please don't ShowBalls. :P