Ah, right, much clearer now that I see this code.
The issue is that a request to an HTTP URL via something like URLLoader
is asynchronous, so the SWF script is not held up waiting for the
network to respond. This means you will need to wait until the response
comes back some time later, i.e. when the result handler is called,
before accessing the generateXML property.
So, I would architect this a little differently.
Decouple the logic for loading XML into a service helper class and then
when you get back a response pass that to a utility method to actually
generate the XML.
-Original Message-
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Suzy Lawson
Sent: Monday, June 05, 2006 11:52 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: Instantiation and setting problems - Example
included
Thanks for your response...this error is still haunting so I included
a simple example to show my problem of class variables not being able to
be set after I initialize a new class. Even if I move the "generateXML"
method into the complete Handlerthe same null pointer is still
thrown. Any ideas why??
Top-level MXML file:
http://www.adobe.com/2006/mxml"
layout="absolute">
_-
//TestA class
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class TestA
{
private var sampleXML : XML ;
public function TestA() : void
{
var loader : URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(new URLRequest("sample.xml"));
}
private function completeHandler(event:Event):void {
sampleXML = new XML(event.target.data);
}
public function generateXML(value : String) : XML
{
[EMAIL PROTECTED] = value; // error is here b/c
sampleXML is 'null'
return sampleXML;
}
}
}
sample.xml file:
--
The exact error I get is:
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
at TestA/generateXML()[C:\flex\TestA.as:25]
at sampleBug/sampleBug::setValue()[C:\flex\sampleBug.mxml:8]
at sampleBug/___Button1_click()[C:\flex\sampleBug.mxml:12]
--- In flexcoders@yahoogroups.com, "Peter Farland" <[EMAIL PROTECTED]> wrote:
>
> I'd have to see the code to really understand what is going on, but
can you avoid doing anything in a constructor and use a button or
something to trigger the work to see if that's the issue?
>
> -Original Message-
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED]
On Behalf Of Suzy Lawson
> Sent: Friday, June 02, 2006 4:54 PM
> To: flexcoders@yahoogroups.com
> Subject: [flexcoders] Re: Instantiation and setting problems
>
> I'm using the Cairngorm framework, so in the implented Command
classes...
>
> For the XML problem, it's in a class that the Command class calls.
The constructor load the XML file stored locally, then the
completeHandler calls XML(event.target.data) and assigns the XML to a
local variable.
>
> However, when I instantiate this object, then call a method on it,
the XML is still null. If I put a timer on it to stall for a second,
it's there
>
> --- In flexcoders@yahoogroups.com, "Peter Farland" wrote:
> >
> >
> > Where are you declaring these classes?
> >
> >
> > -Original Message-
> > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED]
> On Behalf Of Suzy Lawson
> > Sent: Friday, June 02, 2006 12:01 PM
> > To: flexcoders@yahoogroups.com
> > Subject: [flexcoders] Instantiation and setting problems
> >
> > I anyone else having issues trying to set values on objects for
> newly instantiated objects in an object graph with 3 or more layers?
> >
> > I have the following:
> >
> > ClassA {
> > public var classB : ClassB;
> > public var classC : ClassC;
> > }
> >
> > ClassB {
> > public var classD : ClassD;
> > }
> >
> > ClassC {
> > public var textVal : String='';
> > }
> >
> > and i have a value sets in the ModelLocator (cairngorm) class like
so:
> > public var allClassAs : ArrayCollection; public var selectedClassA :
> ClassA;
> >
> > The problem is if I retrieve a single 'ClassA' from the collection
> of allClassAs using the ID of the selectedClassA, then try to make a
call like this:
> > classA.classC.text="Whatever";
> >
> > The mxml field I have bound to that value never displays and I get
> the following debug error.
> >
> > error during evaluation
> > no such variable: classA.classC.textVal
> >
> > This kind of makes sense since classC wasn't initialized yet, so
> even if I change ClassA to be:
> > ClassA {
> > public var classB : ClassB =