2010-04-20 35 views
1

我有2個文件,我的應用程序和一個自定義組件。 在我的組件中,我有一個httpservice和一個名爲_requestUrl的可綁定的字符串。 httpservice使用此。將參數傳遞給flex中的組件

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

在我的應用程序文件,使我的在onCreationComplete功能組件的實例。

在此功能中,如果我說

mycomponent._urlRequest ="http://example.com"httpservice拋出一個null url error但如果我說mycomponent.srv.url="http://example.com"它工作正常。

這是爲什麼?

編輯:

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

回答

1

因爲當組件被初始化,您_requestUrl反正一開始爲空,這就是爲什麼你得到這個錯誤。並且您的srv的url在初始化時綁定爲空值。

的Flex創建分階段成分,因此,如果您在creationComplete等設置變量,creationcomplete被調用後它已經完全創建的組件,它的類的初始化數millseconds後調用。

所以在初始化時,默認情況下都爲空,除非你初始化它內聯初始化表達式如下

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

即便在我第一次感到迷惑,但在Flex的背景下,初始化事件之前,「叫CreationComplete「,在正常的編程環境中,我們認爲我們稍後創建並初始化對象。

在你的榜樣,結合工作開始之前,甚至「creationComplete」之稱,導致它報告空指針異常,所以在此事件之前,你的對象的屬性是任何空的權利。

+0

我不明白?我在嘗試設置字符串之前創建組件?我編輯了我的問題。 它不是一個巨大的交易或任何東西....我的應用作品。它只會bug我,如果我不知道:) – dubbeat 2010-04-20 12:13:47

+0

我希望我的解釋現在對你有用。 – 2010-04-20 18:29:31

+0

太好了。非常感激! – dubbeat 2010-04-22 12:48:10