开发者

How to solve TypeError: Error #1009: Cannot access a property or method of a null object reference while calling swf from flex 3

I'm new to flash . I have called this flash code in Flex using SWF loader .I got the following Error. Here I've attached flash coding for header_text_fla Kindly do the needful.

TypeError: Error #1009: Cannot access a property or method of a null object reference. at header_text_fla::MainTimeline/loaderComplete()

var myEvent:Event;
var myParams:LoaderInfo;
function loaderComplete(myEvent:Event)
{
if(this.loaderInfo.parameters.text1 != null)
{
Text1.text=this.loaderInfo.parameters.text1 as String;
}
if(this.loaderInfo.parameters.text2 != null)
{
Text2.text=this.loaderInfo.parameters.text2 as String;
}
if(this.loaderInfo.parameters.text3 != null)
{
Text3.text=this.loaderInfo.par开发者_高级运维ameters.text3 as String;
}
}

function useParams()
{

}

var myLoaderInfo=new Object();
myLoaderInfo.myParamsLoaded=false;
myLoaderInfo.loaderComplete=loaderComplete;
myLoaderInfo.useParams=useParams;

this.loaderInfo.addEventListener(Event.COMPLETE, myLoaderInfo.loaderComplete );


I have solved by using try catch.Here is the code

try
{
    Text1.text=this.loaderInfo.parameters.text1 as String;
    Text2.text=this.loaderInfo.parameters.text2 as String;
    Text3.text=this.loaderInfo.parameters.text3 as String;
}
catch(error:Error)
{
    trace("Error");
}


:-) Nice code.

Please write:

function loaderComplete(myEvent:Event)
{
if (!this.loaderInfo) trace ("no loader info");
if (!this.loaderInfo.parameters) trace ("no loader info parameters");
if (!Text1) trace ("no Text1");
if (!Text2) trace ("no Text2");
if (!Text3) trace ("no Text3");
...
YOUR CODE HERE
...

This will give you the object that is not initialized and responsible for the error thrown.


What could be the problem is that you are using a temporary object's (myLoaderInfo) function as an event handler. Since the object is temporary (or at least seems to be in the code given above), and the event is sent asynchronously, the garbage collector might clean the myLoaderInfo instance before the function is called making it null and causing the error when the callback function is called.

Try making your myLoaderInfo a class parameter :

var myEvent      : Event;
var myParams     : LoaderInfo;
var myLoaderInfo : Object;//added myLoaderInfo here

function loaderComplete(myEvent:Event)
{
    if(this.loaderInfo.parameters.text1 != null)
    {
        Text1.text=this.loaderInfo.parameters.text1 as String;
    }
    if(this.loaderInfo.parameters.text2 != null)
    {
        Text2.text=this.loaderInfo.parameters.text2 as String;
    }
    if(this.loaderInfo.parameters.text3 != null)
    {
        Text3.text=this.loaderInfo.parameters.text3 as String;
    }
}

function useParams()
{
}

myLoaderInfo = new Object();
myLoaderInfo.myParamsLoaded = false;
myLoaderInfo.loaderComplete = loaderComplete;
myLoaderInfo.useParams      = useParams;

this.loaderInfo.addEventListener(Event.COMPLETE, myLoaderInfo.loaderComplete );

That way, the garbage collector won't clean it up.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜