dubbeat
dubbeat

Reputation: 7847

passing parameters to component in flex

I've got 2 files, my Application and a custom component. In my component I have a httpservice and a string named _requestUrl that is bindable. The httpservice uses this.

<mx:HTTPService id="srv"
                    url="{_requestUrl}"
                    result="parseHttpResult(event)"
                    resultFormat="xml"
                    method="GET"
                    useProxy="false">

In my application file I make an instance of my component in the onCreationComplete function.

In this function if I say

mycomponent._urlRequest ="http://example.com" the httpservice throws a null url error but if I say mycomponent.srv.url="http://example.com" it works fine.

Why is this?

EDIT:

<mx:Script>
    import mx.events.FlexEvent;
    import components.custom
    private var comp:custom= new custom()
    private var comp:custom= new custom()

    public function setVars(event:FlexEvent):void
    {
        comp._requestUrl = "http://example.com"
        comp.setVars(event)
        pform.addChild(comp)
    }
    //creationComplete="setVars(event)"
</mx:Script>

Upvotes: 1

Views: 314

Answers (1)

Akash Kava
Akash Kava

Reputation: 39916

Because when components are initialized, your _requestUrl is null in the beginning anyway, thats why you get this error. And your url of srv is bound to null value on initialization.

Flex creates components in phases, so if you set variable in creationComplete etc, creationcomplete is called after it has completely created the components, it is called after few millseconds of the initialization of class.

So at time of initialization, by default everything is null except you initialize it inline init expression as below

// this will not be null...
var myUrl:String = "my url";

// this will be null
var myUrl2:String;
// binding above value may give null exception
// because it is null at time of initialization

Even to me first time it was confusing but in Flex's context, Initialized event is called before "CreationComplete" and in normal programming context, we think that we create and initialize object later.

In your example, binding starts working even before "creationComplete" is called that causes it to report null pointer exception, so before this event, your object's property is any way null right.

Upvotes: 1

Related Questions