addChild problem

5 views
Skip to first unread message

mitch

unread,
May 27, 2009, 2:04:27 PM5/27/09
to Fluint Discussions
In this simple example which is based on the samples:

override protected function setUp():void
{
try {
setupModel();

modelerMain = new ModelerMain();
modelerMain.addEventListener(FlexEvent.CREATION_COMPLETE,
asyncHandler(handleSetupComplete, 5000), false, 0, true);
addChild(modelerMain);
} catch (e: Error) {
}
}

following it in the debugger, addChild is throwing a null pointer
exception. modelerMain is NOT null. (It is a class in my application
that extends VBox). handleSetupComplete is a function I have defined,
but it is never called.

I'm at a loss to figure out why the exception is being thrown, and why
handleSetupComplete is not being called. Any ideas? Thanks.

Michael Labriola

unread,
May 27, 2009, 2:20:20 PM5/27/09
to Fluint Discussions
mitch,

can you post the whole function definition for handleSetupComplete and
the fill error message.

Thanks,
mike

Michael Labriola

unread,
May 27, 2009, 2:22:49 PM5/27/09
to Fluint Discussions
Also, just to help narrow down the issue, what if you just
instantiation a UIComponent for the moment.. does that work?

modelerMain = new UIComponent();
modelerMain.addEventListener(FlexEvent.CREATION_COMPLETE,
asyncHandler(handleSetupComplete, 5000), false, 0, true);
addChild(modelerMain);


> > handleSetupComplete is not being called.  Any ideas?  Thanks.- Hide quoted text -
>
> - Show quoted text -

mitch

unread,
May 27, 2009, 2:41:24 PM5/27/09
to Fluint Discussions
I changed it to this:

text = new Text();
text.addEventListener(FlexEvent.CREATION_COMPLETE,
asyncHandler(handleSetupComplete, 5000), false, 0, true);
addChild(text);

and there was no exception, and handleSetupComplete() was called
correctly. Its definition is just:

protected function handleSetupComplete(event:FlexEvent,
passThroughData:Object):void
{
//This method will be called after the TextInput is created. The
TextInput will be ready to use
//before any of our test methods run
var x:int = 3;
}


When I go back to using modelerMain, the error is a TypeError and the
text is "Error #1009: Cannot access a property or method of a null
object reference."

So I guess something is null but not modelerMain itself.


Michael Labriola

unread,
May 27, 2009, 2:44:18 PM5/27/09
to Fluint Discussions
I am guessing something in modelerMain is either trying to get its
parent or potentially access something before it is ready.

mitch

unread,
May 27, 2009, 2:46:36 PM5/27/09
to Fluint Discussions
ModelerMain is a VBox and it contains some nested components like
labels and buttons. These things are null, when looked at in the
debugger, after the return from new ModelerMain. They shouldn't be.
And I don't understand why handleSetupComplete would not be called.

Jeff Tapper

unread,
May 27, 2009, 3:36:42 PM5/27/09
to Fluint Discussions
Mitch - remember, that the component lifecycle needs to run for all
components, so you need to make sure you are listening for the
creationComplete event of each component you instantiate and add to
the display list before you try and reference its children. If the
handleSetupComplete method isnt being fired, it seems likely that your
modelerMain may be throwing an exception before its creationComplete
event is fired. You may want to add an event listener for its
preinitialize and initialize events, to help pinpoint exactly when it
is failing

mitch

unread,
May 27, 2009, 4:42:33 PM5/27/09
to Fluint Discussions
That's a good idea. I added listeners and now am finding that it's
not even getting to preinitialize.

This is making me realize that I don't have a very deep understanding
of how Flex components are created and initialized. I never needed it
before but now to understand FLUINT it seems necessary.

Stepping back a little, here's the approach I'm trying to take to
write a GUI test:
- create a lot of data and put it into my program's Model, so it
hopefully has all the same data as if it were connected to the server
at startup
- create a GUI object, in this case ModelerMain
- write my test to look at the contents of various things that will be
displayed in ModelerMain, comparing them with expected values.

I had sort of assumed that if I got all the data in the model right,
which was quite a bit of work, that I could just do new ModelerMain()
and it would work by getting its data from the Model. That isn't
happening so far. I'm willing to debug to try to figure out why not,
but I'm having a hard time even getting into the code to debug what's
going wrong.

Michael Labriola

unread,
May 27, 2009, 4:51:51 PM5/27/09
to Fluint Discussions
Mitch,

I think you likely have one misconception which is at the root of all
of this.

When you say modelerMain = new ModelerMain(); the children of
ModelerMain are not created. That isn't how the Flex component
lifecycle works. Those children are created some number of cycles
later after the modelerMain is added to the displayList.

We can discuss the approach next, but my guess is you are likely
trying to access one of those visual children either in your
constructor or shortly thereafter assuming they will be ready. Is that
a valid guess?

Mike

mitch

unread,
May 27, 2009, 5:14:33 PM5/27/09
to Fluint Discussions
That's a valid guess.

Suppose ModelerMain is just a tiny bit complicated, say it is a VBox
containing 2 TextFields and 1 Button. (The actual thing I'm trying to
test has a lot more than that.) Is there a way to create it that will
work with FLUINT, and will also allow me to keep the original
ModelerMain source code without rewriting it?

Thanks a lot for answering all the questions, I appreciate it.

- Mitch

Michael Labriola

unread,
May 27, 2009, 5:36:00 PM5/27/09
to Fluint Discussions

Well, its not so much Fluint as a way to create it that it will work
with Flex reliably.

Honestly though, it might just be that there is fundamental issues
with ModelerMain that should be corrected. If you can't instantiate
the class and add it to a display list without a crash, you aren't
going to be able to test it.

You might want to take a few and just read about the component
lifecycle in Flex. Honestly, 90% of writing good tests just comes down
to making sure the architecture is right during the actual building of
the components. I will be glad to take a look and provide suggestions
if you can provide code.

Mike
Reply all
Reply to author
Forward
0 new messages