2010-09-08 89 views
2

我有一個DropdownList,顯示供應商列表&必須選擇與該Patient關聯的Provider。Flex下拉列表CreationComplete錯誤

的下拉列表:

<s:DropDownList id="providerList" 
     width="80%" 
     fontSize="12" 
     fontWeight="bold" 
     selectionColor="white" 
     creationComplete="providerList_creationCompleteHandler(event)" 
     dataProvider="{model.practiceProviderList.practiceProviders}"/> 

其中practiceProviders是一個ArrayCollection

的CreationCompleteHandler功能:

protected function providerList_creationCompleteHandler(event:FlexEvent):void 
    { 
    var firstN:String; 
    var lastN:String; 
    var providerObj:Provider = new Provider(); 

    if (model.patientDetails.patientDetail.patientProviders != null && model.patientDetails.patientDetail.patientProviders.length > 0) 
    { 
    firstN = patientDetailsModel.patientDetails.patientDetail.patientProviders.getItemAt(0).provider.providerName.firstName; 
    lastN = patientDetailsModel.patientDetails.patientDetail.patientProviders.getItemAt(0).provider.providerName.lastName; 

for (var count:int = 0; count < patientDetailsModel.practiceProviderList.practiceProviders.length; ++count) 
    { 
providerObj = patientDetailsModel.practiceProviderList.practiceProviders.getItemAt(count, 0).provider as Provider; 

if (providerObj.providerName.firstName == firstN && providerObj.providerName.lastName == lastN) 
     { 
this.providerList.selectedIndex = count; 
     } 
    } 
    } 
    } 

的問題是,當我去這個頁面的第一次,錯誤是:

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at com.newwavetechnologies.modules::demographics/providerList_creationCompleteHandler()[C:\harish\flex\apps\workspace\dataCollection-flexUserInterface\src\com\newwavetechnologies\modules\demographics.mxml:166] 
at com.newwavetechnologies.modules::demographics/__providerList_creationComplete()[C:\harish\flex\apps\workspace\dataCollection-flexUserInterface\src\com\newwavetechnologies\modules\demographics.mxml:359] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\core\UIComponent.as:12266] 
at mx.core::UIComponent/set initialized()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1577] 
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:759] 
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1072] 

其中線166是:

if (providerObj.providerName.firstName == firstN && providerObj.providerName.lastName == lastN) 

的providerObj是空的第一次。但是,當再次回到同一頁面時,一切正常,並且列表中的一個提供者被正確選擇。

可能我第一次在創建List之前調用creationComplete處理函數。第二次調用時,列表被填充並且處理程序正常工作。如果有人能在這方面幫助我解決這個問題,那將是非常棒的。

感謝

哈里什

回答

3

這很難說是怎麼回事,但問題就出在這裏:

providerObj = patientDetailsModel.practiceProviderList.practiceProviders.getItemAt(count, 0).provider as Provider; 

有在該行可能發生空指針異常的地方一噸。

最有可能 - 返回practiceProvider位置count沒有供應商組。我們無法看到這個值是如何填充的,但是鑑於此代碼稍後有效,我會說你的競爭狀況正在發生 - 數據在被設置之前被訪問。

至少,你應該添加一個guardClause此:

var practiceProviders:ArrayCollection = patientDetailsModel.practiceProviderList.practiceProviders; 
for (var count:int = 0; count < practiceProviders.length; ++count) 
{ 
     providerObj = practiceProviders.getItemAt(count, 0).provider as Provider; 
     if (!providerObj) 
     continue; 
     // etc 
} 

競爭狀態是有點麻煩,因爲Flex服務器調用的asyncronous natoure。 (我假設你正在從遠程服務器加載數據)。

有解決此兩種方法 - 要麼這個方法的

  • 推遲執行,直到數據加載 - 你可以通過添加事件監聽到RemoteService的ResultEvent

或做

  • 不要擔心它第一次,但重新執行方法,只要數據發生變化。

如:

protected function providerList_creationCompleteHandler(event:FlexEvent):void 
{ 
    dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE,onCollectionChange,false,0,true); 
    updateProviders(); 
    // Rest of existing creationComplete code moved to updateProviders(); 
} 
private function updateProviders() 
{ 
     // Code from existing creationComplete handler goes here 
} 
private function onCollectionChange(event:CollectionEvent):void 
{ 
     updateProviders(); 
} 
+0

馬蒂感謝您的答覆,它擺脫了錯誤的。但是第一次沒有選擇提供商。下一次它。所以仍然沒有解決我的問題:(有沒有辦法來解決競爭條件。對不起,但我是新來的Flex世界! – Harry 2010-09-08 16:41:10

+0

我已經更新了我的答案,可能的解決方案。 – 2010-09-08 17:57:20

+1

馬蒂,謝謝。我想知道是什麼原因導致了錯誤,providerObj不是Bindable,所以我添加了這個和瞧!我將把你的答案標記爲正確的:)。非常感謝!!! – Harry 2010-09-08 18:14:36