Object reference not set to an instance of an object
by Stuart Walton · in Torque X 2D · 05/08/2008 (10:49 am) · 4 replies
Im just trying to add life icons to the game im working on based on the Blaster tutorial code for an assignment.
Using code from Spaceshooter that generates the life icon sprites implimented into my game.cs im getting an error on line:
_life1 = (T2DSceneObject)(TorqueObjectDatabase.Instance.FindObject("PlayerShipIcon") as T2DStaticSprite).Clone();
The error is:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
What does this error mean? i also got it yesterday when trying to add sound to the game.
What do i need to do to correct this error?
Using code from Spaceshooter that generates the life icon sprites implimented into my game.cs im getting an error on line:
_life1 = (T2DSceneObject)(TorqueObjectDatabase.Instance.FindObject("PlayerShipIcon") as T2DStaticSprite).Clone();
The error is:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
What does this error mean? i also got it yesterday when trying to add sound to the game.
What do i need to do to correct this error?
About the author
#2
I thought origionaly it was trying to find something that didnt exist within the code
05/08/2008 (11:35 am)
Cheers fixed it by creating an object in the scene called PlayerShipIconI thought origionaly it was trying to find something that didnt exist within the code
#3
(T2DSceneObject)(TorqueObjectDatabase.Instance.FindObject("PlayerShipIcon") as T2DStaticSprite)
results in a null value, which you are then trying to call the Clone() method on, which (for obvious reasons) fails. Also, there is no reason to do a double cast. the (type) cast operator and the as keyword do the same thing, so just do this:
(TorqueObjectDatabase.Instance.FindObject("PlayerShipIcon") as T2DSceneObject)
05/08/2008 (6:50 pm)
Technically it isn't that _life1 is null, but that this:(T2DSceneObject)(TorqueObjectDatabase.Instance.FindObject("PlayerShipIcon") as T2DStaticSprite)
results in a null value, which you are then trying to call the Clone() method on, which (for obvious reasons) fails. Also, there is no reason to do a double cast. the (type) cast operator and the as keyword do the same thing, so just do this:
(TorqueObjectDatabase.Instance.FindObject("PlayerShipIcon") as T2DSceneObject)
#4
05/09/2008 (8:01 am)
@Russell - I was trying to laymen term it for him since he obviously isn't a strong programmer based on his initial post =P
Torque 3D Owner Jonathon Stevens
You need to step through the code to see what the issue is and get more details.