Sorry, although we've made it easy to write components in either MXML or
AS3, we haven't done the same with Application. If you are a newbie you
should start out with <mx:Application> in MXML. You can then use AS3 for
all your components if you really want to. I hope we can make this
better in a future release.

 

What's going on is that when the MXML compiler compiles an
<mx:Application> it autogenerates code to set up a lot of stuff such as
the default CSS type selectors that various components like Button
depend on. These aren't getting set up when you simply extend
Application as an AS3 class. When Button calls getStyle() to find out
what it's skin class should be, it's getting a null Clas reference.

 

Once you are no longer a newbie, you can use mxmlc's
-keep-generated-actionscript option to see all the generated code, and
set this stuff up yourself it you insist. But I really don't understand
why some developers like AS3 so much better tham MXML. Would you write a
web app all in JS and no HTML?

 

- Gordon

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of bookymcnuggett
Sent: Thursday, November 16, 2006 5:49 PM
To: [email protected]
Subject: [flexcoders] Newbie: Why can't I add a button!

 

Hi All,

This is my first posting to this forum.

I have taken my first newbie steps to creating a stand-alone AS3 
Application and have come across something I cannot understand. I 
have an Application, and add an Image to it and it is fine. I add a 
Button to it and it blows up with the AS3 equivalent of a null 
pointer exception. The code is below. The error is below that. If I 
comment out the line that does the "addChild(button)" it correctly 
adds and displays the Image. What am I doing wrong? What is wrong 
with how I am adding a button? Both Button and Image descend from 
UIComponent.

package {

import mx.core.*;
import mx.events.*;
import mx.skins.halo.*;
import mx.styles.*;
import mx.controls.*;
import flash.display.*;

public class FirstApp extends Application
{
[Embed(source="connected_multiple_big.jpg")]
private var ImageClass:Class;

public function FirstApp()
{
super();
this.layout = "vertical";
this.setStyle("borderSkin",mx.skins.halo.HaloBorder);
this.addEventListener(FlexEvent.APPLICATION_COMPLETE, doInit); 
}

private function doInit(e:FlexEvent):void {
var img:Image = new Image();
img.setStyle("verticalAlign","top");
img.source = ImageClass; 
addChild(img);

// the troublesome test button. addChild() here causes a null object 
reference exception
var button:Button = new Button();
button.label = "label";
addChild(button); 

}
}
}
/// ****************************

TypeError: Error #1009: Cannot access a property or method of a null 
object reference.
at mx.core::UIComponent/getStyle()
at 
mx.controls::Button/http://www.adobe.com/2006/flex/mx/internal::viewS
<http://www.adobe.com/2006/flex/mx/internal::viewS> 
kinForPhase()
at 
mx.controls::Button/http://www.adobe.com/2006/flex/mx/internal::viewS
<http://www.adobe.com/2006/flex/mx/internal::viewS> 
kin()
at mx.controls::Button/mx.controls:Button::commitProperties()
at mx.core::UIComponent/validateProperties()
at mx.managers::LayoutManager/::validateProperties()
at mx.managers::LayoutManager/::doPhasedInstantiation()
at Function/http://adobe.com/AS3/2006/builtin::apply
<http://adobe.com/AS3/2006/builtin::apply> ()
at mx.core::UIComponent/::callLaterDispatcher2()
at mx.core::UIComponent/::callLaterDispatcher()
at 
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchE
ventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.managers::SystemManager/::preloader_preloaderDoneHandler()
at 
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchE
ventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::Preloader/::displayClassCompleteHandler()
at 
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchE
ventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::DownloadProgressBar/::timerHandler()
at mx.preloaders::DownloadProgressBar/::initCompleteHandler()
at 
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchE
ventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::Preloader/::dispatchAppEndEvent()
at mx.preloaders::Preloader/::appCreationCompleteHandler()
at 
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchE
ventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/set initialized()
at mx.managers::LayoutManager/::doPhasedInstantiation()
at Function/http://adobe.com/AS3/2006/builtin::apply
<http://adobe.com/AS3/2006/builtin::apply> ()
at mx.core::UIComponent/::callLaterDispatcher2()
at mx.core::UIComponent/::callLaterDispatcher()

 

Reply via email to