Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:48:05+00:00 2026-06-08T06:48:05+00:00

I am building a Flex application with a problem I am having with detecting

  • 0

I am building a Flex application with a problem I am having with detecting whether or not a variable has been initialized. Observe this simplified code sample:

<fx:Script>
  <![CDATA[
    private var pageObject:* = null; //Yes the "*" data type is needed ;)

  //Fired by the application elseware
    private function constructMenu(e:ResultEvent):void {
      if (this.pageObject != null) {
        //This block never runs...

        // The pageObject will be null when the application
        // first runs, so skip this first time around.
        // However... this method will be called multiple
        // times. Since FoodMenu() is a display object
        // and is instantiated below, all subsequent calls
        // to this method will require us to remove the old
        // display object before adding a new one.
      } else {
        // This block always runs...
      }

      setTimeout(function():void {
        this.pageObject = new FoodMenu(); //Should now be not-null!!!

        //Do more stuff with the FoodMenu() and add to the main application
      }, 1000);
    }
  ]]>
</fx:Script>

Based on the given code sample and comments, could someone please explain why this.pageObject always registers as not null, regardless of how many times the constructMenu() method is called or how many times the FoodMenu() class is instantiated?

Thank you for your time.

Edit: the full MXML as requested

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
           xmlns:mx="library://ns.adobe.com/flex/mx"
           xmlns:components="components.*"
           xmlns:pagesservice="services.pagesservice.*"
           xmlns:menuservice="services.menuservice.*"
           minWidth="955" minHeight="600" backgroundColor="0x141414"
           creationComplete="init(event)" skinClass="skins.Theme" xmlns:pages="components.pages.*" xmlns:reviewsservice="services.reviewsservice.*">
<fx:Script>
    <![CDATA[
        import com.asual.swfaddress.*;
        import com.forwardfour.boncuisson.events.MenuEvent;
        import com.greensock.TweenMax;
        import com.greensock.plugins.VisiblePlugin;

        import components.header.Menu;
        import components.pages.FoodMenu;

        import flash.utils.setTimeout;

        import mx.controls.Alert;
        import mx.core.IVisualElement;
        import mx.events.FlexEvent;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        import skins.Theme;

        import spark.components.Group;

    //Globalize a reference to the navigation menu
        private var menu:Menu;

    //Globalize a reference to the page content
        private var pageObject:* = null; //This reference will be various types, so just make a general object

    /**
     * Initialization
     * -------------------------------
    */

    //Add an event listener for when "links" on the menu are clicked and initialize SWFAddress
        private function init(event:FlexEvent):void {
        //Listen for menu clicks
            var skin:Theme = Theme(this.skin);
            this.menu = Menu(skin.menu);
            this.menu.addEventListener(Menu.MENU_ITEM_CLICKED, menuNavigateToPageHandler);

        //Initialize SWFAddress
            SWFAddress.addEventListener(SWFAddressEvent.INIT, initSWFAddress);
        }

    //Initialize SWFAddress and grab the page listed from the URL
        private function initSWFAddress(e:SWFAddressEvent):void {
        //Listen for change events
            SWFAddress.addEventListener(SWFAddressEvent.CHANGE, URLNavigateToPageHandler);

        //Fetch the page from the URL
            var URL:String = SWFAddress.getValue();

            if (URL == "" || URL == "/") {
                pagesResponder.token = page.getPagesByPosition(1);
            } else {
                pagesResponder.token = page.getPagesByURL(URL.substring(1));
            }
        }

    /**
     * Navigation handlers
     * -------------------------------
    */

    //Go to a specific page when a menu item has been clicked
        private function menuNavigateToPageHandler(e:MenuEvent):void {
            SWFAddress.setValue(e.pageURL);
            pagesResponder.token = page.getPagesByID(e.pageID);
        }

    //Request a page when the URL has changed
        private function URLNavigateToPageHandler(e:SWFAddressEvent):void {
            var URL:String = SWFAddress.getValue();

            if (URL == "" || URL == "/") {
                pagesResponder.token = page.getPagesByPosition(1);
            } else {
                pagesResponder.token = page.getPagesByURL(URL.substring(1));
            }
        }   

    /**
     * Loading content
     * -------------------------------
    */  

    //Show an error dialog in the case of an error when communicating with the server
        private function requestErrorHandler(e:FaultEvent):void {
            Alert.show("Fault string: " + e.fault.faultString + "\nFault detail: " + e.fault.faultDetail, e.fault.faultCode);
        }   

    //Determine what kind of page will be constructed
        private function determinePage(e:ResultEvent):void {
            var pageType:String = pagesResponder.lastResult.type;

        //We will need to know the type of page to build
            switch(pageType) {
                case "menu" : 
                    menuResponder.token = menuFetch.getMenuByType(pagesResponder.lastResult.category);
                    break;

                case "lunch" : 
                    break;

                case "reviews" : 
                    reviewsResponder.token = reviews.getAllReviews();
                    break;

                case "home" : 
                default : 
                    break;
            }
        }

    //Using the data that was fetched from the "menuFetch" service, construct the page of type menu
        private function constructMenu(e:ResultEvent):void {
        //Transition the existing page out of view
            if (this.pageObject != null) {
                Alert.show("i");
                TweenMax.to(this.pageObject, 0.75, {
                    alpha : 0,
                    y : 20
                });


                removeElement(this.pageObject);
            }

            setTimeout(function():void {
                this.pageObject = new FoodMenu();
                this.pageObject.alpha = 0;
                this.pageObject.y = 20;
                this.pageObject.data = menuResponder.lastResult;
                addElement(this.pageObject);

            //Tween the new menu into place
                TweenMax.to(this.pageObject, 0.75, {
                    alpha : 1,
                    y : 0
                });
            }, 1000);
        }
    ]]>
</fx:Script>

<!-- Make a request to the server for the page data -->
<fx:Declarations>
    <pagesservice:PagesService id="page"/>
    <s:CallResponder id="pagesResponder" fault="requestErrorHandler(event)" result="determinePage(event)"/>

    <menuservice:MenuService id="menuFetch"/>
    <s:CallResponder id="menuResponder" fault="requestErrorHandler(event)" result="constructMenu(event)"/>

    <reviewsservice:ReviewsService id="reviews"/>
    <s:CallResponder id="reviewsResponder" fault="requestErrorHandler(event)" result="constructMenu(event)"/>
</fx:Declarations>
</s:Application>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:48:06+00:00Added an answer on June 8, 2026 at 6:48 am

    One possible problem I see is with this piece of code :

    setTimeout(function():void {
       this.pageObject = new FoodMenu(); //Should now be not-null!!!
       //Do more stuff with the FoodMenu() and add to the main application
    }, 1000);
    

    The “this” pointer refers to something else(possibly the global object) in the closure.

    Try the following to see for your self :

    trace(this);
    setTimeout(function():void {
        trace(this);
    },1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Flex/Flash Builder 4 application that loads data using E4X/XML, like this:
I am building a mobile application in Flex 4.5 and from this app I
I'm building a flex application It has a functionality that can upload files to
I'm building Flex 3 application and I have a geocoding service ( http://.../GeocodeServer )
I am building a Flex application with ActionScript 3. Unfortunately I've hit a wall
I'm building a Flex application, which is going to perform many server requests (let's
I'm building a quite large Flex application as our administration tools and I need
I'm building a Flex 4 + Rails 2.3.5 application. First I was using XML
So i am building an application on flex for a client and he asked
I am building a compartmentalized Flex application which uses <SWFLoader > to load additional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.