FLEX + Calling Function From Main MXML

Posted on 2010-01-08
Last Modified: 2012-05-08
I have a MXML Component and want to call a function inside it from the main MXML but i am getting the follow error:

Error #1009: Cannot access a property or method of a null object reference.

the error is from the is line of code (see full code attached)

inside main MXML file

import components.Utilization;

private var app:Utilization;

            private function init(evt:Event):void {
                /* Set up full screen handler. */
                Application.application.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandler);
                var myDate:Date = new Date(); 
				var reportDate:Date = new Date(myDate.getTime() + (-1 * millisecondsPerDay));
				var newdate:String = dateFormatter.format(reportDate); 
                dtReport.text = newdate;

            private function fullScreenHandler(evt:FullScreenEvent):void {
                 if (evt.fullScreen) {
                    btnFull.label = "Normal";
                } else {
                    btnFull.label = "Fullscreen";

inside the component file here is the function

			    public function populateUtilization():void
                    var service:WebService = new WebService();
                    service.addEventListener(ResultEvent.RESULT, utilizationResultHandler);
                    service.addEventListener(FaultEvent.FAULT, serviceFaultHandler);

Open in new window

Question by:dkilby
    LVL 19

    Expert Comment

    can't tell if this is it, as i suspect there's code that's not posted, but...

    i see app declared:

    private var app:Utilization;

    and i see the method call:


    but i don't see it instantiated anywhere?

    shouldn't there be a app = new Utilization() or something similar somewhere?


    Author Comment

    where should the instantiation line go? just before when i call the function?  Is there a way i can make sure the component is loaded before calling it? or loading it somewhere so when the function is called i know it was called correctly?
    LVL 19

    Accepted Solution

    "where should the instantiation line go? just before when i call the function?"

    sure.  the instantiation can happen anywhere - you can try putting it on the line just before you call it's methods.

    using a more common class, Array, here's how it works:

    right now, you're basically doing this:

    var arr:Array;

    you declared it:

    var arr:Array;

    meaning "i'm going to create a variable for an array"

    but you never defined it

    since arr hasn't been defined, it'll give an error

    you need to define - or "instantiate" it:

    var arr:Array;
    arr = new Array("c","b","a");

    now "arr" exists so it can be sorted.

    "Is there a way i can make sure the component is loaded before calling it?"

    a component isn't technically an external file, so there's no load operation, so you don't need to wait for it to load.

    "or loading it somewhere so when the function is called i know it was called correctly?"

    if you want to make sure it got called, in the method itself, put a trace action (or with a simple message like ("yep, this method got invoked").


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Title # Comments Views Activity
    labelFunction 3 261
    TitleWindow position on screenreen in Flex 3 9 474
    Function to return shape (as3) 1 507
    Adobe Flex - Combobox - Disable Editing 15 697
    First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
    Digital marketing agencies have encountered both the opportunities and difficulties that emerge from working with a wide-ranging organizations.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now