View Full Version : parentDocument issues
jrashag
08-07-2008, 02:41 AM
I'm trying use parentDocument to call a function in the what I believe to be as the parent class of my current and it keeps throwing an error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
So details of situation...
I have a.mxml and b.mxml
b.mxml gets created as a component in a.mxml, therefore it would make sense that a is the parent of b....
within b, I need to access a function of a, and when i try to call parentDocument.function(); it throws the error. Why doesn't this work? :/
worthyashes
08-07-2008, 09:24 AM
It can't call the function a as far as flash is concerned it doesn't know the parent has the function available.
One way around it would be to expicitly cast the parent to the correct type. As everything in Flex is Object Orientated everything becomes a class when compiled. So for example if the parent is the main application file and is called ParentTesting.mxml it will be compiled into a class called ParentTesting. So in the child you can cast the parent to the type of ParentTesting and then have access to the function.
Here's a bit of code to demonstrate :
ParentTesting.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
<mx:Script>
<![CDATA[
public function helloWorld():void
{
trace("I'm alive");
}
]]>
</mx:Script>
<local:Test />
</mx:Application>
Test.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
width="400"
height="300"
creationComplete="creationCompleteHandler()">
<mx:Script>
<![CDATA[
private function creationCompleteHandler():void
{
var parentRef:ParentTesting = this.parentDocument as ParentTesting;
parentRef.helloWorld();
}
]]>
</mx:Script>
<mx:Label text="This is just a placeholder so we can visiblty see Test is created"/>
</mx:Canvas>
Hope this helps!
jrashag
08-07-2008, 04:47 PM
that's perfect, thanks for showing me that!
pappadre
01-26-2009, 10:52 PM
What if you have a generic component that will be used by multiple components?
I have such an example where is module is loaded using ModuleManager.getModule(); I can call multiple methods inside the module but when the module attempts to dispatch a custom event using either
1) parent.dispatchEvent(...);
2) parentDocument.dispatchEvent(...);
3) parentApplication.dispatchEvent(...);
it fails with the same error i.e. the object is NULL.
wvxvw
01-27-2009, 08:16 AM
This is because you're not supposed to call dispatchEvent on anything that isn't "this"... i.e. something.dispatchEvent(...) looks already weird... why would you need to do that?
Also, considering the component's life circle, you're probably not calling it in the right time, you should do it after the component's construction is complete, in creationComplete handler for example.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.