2 Replies Latest reply on Oct 28, 2009 9:05 AM by timo888

    error when following devnet code-behind example

    timo888 Level 1

      I want to use the designer to build the component but also pass the constructor some arguments, so code-behind seemed the way to go.  I'm trying to create something analogous to the example here, though my class extends TitleWindow, not Form, because I want it to be a popup:

       

      http://www.adobe.com/devnet/flex/quickstart/building_components_using_code_behind/

       

       

       

      but am getting a null reference error in the creationCompleteHandler -- submitButton is null:

       

       

          private function creationCompleteHandler (event:FlexEvent):void

       

              {
                  submitButton.addEventListener(MouseEvent.CLICK, submitHandler);
              }

       

       


      Yet the submit button is placed in the mxml and assigned an ID there:

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <OEUTIL:ArgsTest xmlns:mx="http://www.adobe.com/2006/mxml"   xmlns:OEUTIL="OEUTIL.*"    >
         
      <mx:Button id="submitButton" label="Button"/>   
      </OEUTIL:ArgsTest>

       

      and variable submitButton is declared public inside the code-behind class (see below).  Should the button's click-eventhandler be wired up in the mxml? It's as though the button hasn't been created before the creationComplete event fires.


       

      package OEUTIL
      {
          import mx.containers.TitleWindow;
          import mx.controls.Alert;   
           import mx.events.FlexEvent;
          import mx.controls.Button;
           import flash.events.MouseEvent;


      public class ArgsTest extends TitleWindow   
          {

       

      public var submitButton:Button;

      private var _arg1: Array;
      private var _arg2:String;

       


      public function ArgsTest(arg1:Array, arg2:String)
              {
                  super();
                  _arg1=arg1;
                  _arg2=arg2;
                 

          
                  addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);

       

              }
             
                     
      private function creationCompleteHandler (event:FlexEvent):void

              {

                              // why is submitButton  null?
                  submitButton.addEventListener(MouseEvent.CLICK, submitHandler);
              }

          

             
      private function submitHandler (event:MouseEvent):void       {
                    Alert.show("arg1 length is " + _arg1.length.toString() + "  arg2=" + _arg2);             
        }