Attempt to Access Null Object - Why?
BlitzMax Forums/BlitzMax Beginners Area/Attempt to Access Null Object - Why?
| ||
Ack! What am I doing wrong here? I get an "Attempt to Access Null Object" error. Type Vector3 Field X:Double Field Y:Double Field Z:Double End Type Type SteeringOutput Field Linear:Vector3 Field Angular:Vector3 End Type Local MySteering:SteeringOutput = New SteeringOutput MySteering.Angular.X = 90 Print MySteering.Angular.X |
| ||
You're creating an instance of SteeringOutput with New but not instances of its two Vector3 types (which need it since Vector3 is also a user-defined type). You can do the Vector3 creation within SteeringOutput's New method if you like:Type Vector3 Field X:Double Field Y:Double Field Z:Double End Type Type SteeringOutput Field Linear:Vector3 Field Angular:Vector3 Method New() Linear = New Vector3 Angular = New Vector3 End Method End Type Local MySteering:SteeringOutput = New SteeringOutput MySteering.Angular.X = 90 Print MySteering.Angular.X |
| ||
Excellent. Thank you! |
| ||
you actually don't need the Method New()....Type SteeringOutput Field Linear:Vector3 = New Vector3 Field Angular:Vector3 = New Vector3 End Typewill work just fine |
| ||
you actually don't need the Method New().... Whilts I aggre with DMaz, and dont use the New in open code at all 1) The method "New" theoreticly already exits. 2) Whislt in this situtation you dont need it, Its good practice, because you can then put all the memory allocation in it. (Ie Tlist if you want) |
| ||
sure... I can understand that but my philosophy is; plan ahead when coding but don't add code unless/until it's needed. I like to keep my code as simple and short as possible. but I don't get your point about TList... it can be allocated just like the vector. or are you talking about adding the new instance to a global list or something? |
| ||
but I don't get your point about TList... it can be allocated just like the vector. or are you talking about adding the new instance to a global list or something? Yep, basicly when you want to use that B3D thing were you add an instance of every object to the global list for that object.(Dont missunderstand I dont use NEW in open code, I always have a create Function, and I have overridden all my "News" so that they dont except being called from anywhere accept their Create Function. Thus I cannot do it the way you recomended. (Which is deliberate). The reason I did this was that originaly I had been doing it your way, but when I came to situations, were I didnt want (in this example), the three doubles to be created, but rather simply wanted the new Type to point to an already existing Instance, rather than create an Instane and the have it auto relesed then GC'ed. Dont forget in Morbius' original example, "Linear" does exist, but it simply points to NULL, and so hasnt takenup any memory (yet) other than the pointer itself, whereas in your example, the Memory is auto allocated, and then possibly discarded) Dont forget These comments are just thoretic discustion to you, and that Morbius could do a lot worse than following either of the two possibilities he his been given, by you and Wendel |
| ||
Great explanations! Thank you. |