开发者

How to find out if a class has been instantiated

开发者 https://www.devze.com 2023-01-26 18:45 出处:网络
I usually use if(object!=null) but it doesn\'t work well. How can I verify if the class is instantiated. I want to get rid of the \'cannot access a property of a null object or reference\'.

I usually use if(object!=null) but it doesn't work well.

How can I verify if the class is instantiated. I want to get rid of the 'cannot access a property of a null object or reference'.

Thanks!

I mean, the 'var object:Object;' is just a reference to an Object class instance. When i initialize it wi开发者_Python百科th 'object = new Object()' it runs the code in the constructor, initializing it. How can I check if it has been initialized or not.


If you really need to know if the class has been initialized then I would recommend you use a getter inside the class that returns a true value when you have completed your initialization. Of course this only works if you have created an instance of the class. If I am not sure if the instance exists I first check if the instance is created. I have never had any problems with it.

if(myObject && myObject.isInitialized)
{
    // success
}


instead of if(myobj!=null), you can just do if(myobj).

checking to see if an instance or property is null is different than checking if it exists.

if(myobj)

will fail if myobj is null or undefined whereas if(myobj!=null) misses undefined (which is what the instance reference would be if it has never been assigned a value).

note: the if(myobj) will also be false if the value is set to false or 0. since you are checking worried about a null pointer exception (therefore using a complex object), you can rule out false and 0 from being viable values (giving you a false negative) and use the more general if statement to check whether or not the instance exists.


You may have a common situation where you start loading some assets in the constructor, let's suppose a background image. This code would not work properly:

var object:TestObject = new TestObject(); //inside the contructor, the background image will start loading
object.background.width = 120; // this will not work, because the background is not loaded yet

There are a lot of ways to solve this...

You could load the assets before creating the object, and after they are loaded completely send them to the constructor as parameters.

Or you could define some properties inside the object which will be assigned to the loaded assets when they complete loading, something like this:

//outside
object.BackgroundWidth = 120;

----------

//inside the class
public var BackgroundWidth:int;
private var background:Bitmap;

public function TestObject(){
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, assignProperties);
    loader.load("img.png");
}
//this will make sure the width is applied to the background 
//when it's loaded completely
private function assignProperties(e:Event){
    background = Bitmap((event.currentTarget as LoaderInfo).content);
    background.width = BackgroundWidth; 
}

I hope this helped.

0

精彩评论

暂无评论...
验证码 换一张
取 消