tags:

views:

49

answers:

1

Hi,

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">

    </mx:HTTPService>

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

In this function if I say

mycomponent._urlRequest ="http://mysite.com" the httpservice throws a null url error but if I say mycomponent.srv.url="http://mysite.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://site.com"

           comp.setVars(event)
           pform.addChild(comp)


        }
        //creationComplete="setVars(event)"
    </mx:Script>
+1  A: 

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.

Akash Kava
I don't understand that? I create the component before I try to set the string? I have edited My Question.Its not a huge deal or anything.... my app works. It will just bug me if I don't know :)
dubbeat
I hope my explation is useful to you now.
Akash Kava
Thats great. Much appreciated!
dubbeat