Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

FLEX + Calling Function From Main MXML

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)

app.populateUtilization();
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";
                    app.populateUtilization();
                }
            }

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);
                    service.loadWSDL("http://localhost/Operations/Operations.asmx?wsdl");
                    service.getUtilization(mainapp.dtReport.text);
                }

Open in new window

0
dkilby
Asked:
dkilby
  • 2
1 Solution
 
moagriusCommented:
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:

app.populateUtilization();

but i don't see it instantiated anywhere?

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

0
 
dkilbyAuthor Commented:
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?
0
 
moagriusCommented:
"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;
arr.sort();

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");
arr.sort();

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 Alert.show) with a simple message like ("yep, this method got invoked").

hth
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now