Dynamically selecting a combo box item (from db)

FlexingCF
FlexingCF used Ask the Experts™
on
I am trying to dynamically select a combobox item from a value stored in a database. The initial combobox values come from an .as file, which is working fine. When I put in the code to trigger the selected item, I get this strange cannot reference a null object or method error.

I will post the .as code for the combobox values, as well as the flex code.

P.S. I am recycling existing, working code, and am not sure why this isn't working...

.as [combobox values]
package com.Evolve.UI.ComboValues
{
	
	import mx.collections.*;
	
	public class DealCB
	{
		public function DealCB()
		{
		}
		
		
		public var combo_RentalTerms:Array = 
	    [{label:"Annually", data:"Annually"},
	    {label:"Quarterly", data:"Quarterly"},
	    {label:"Monthly", data:"Monthly"},
	    {label:"Weekly", data:"Weekly"},
	    {label:"Daily", data:"Daily"}];
	    
 
	}
}
 
.mxml file
OBJECT
<mx:ComboBox id="cb_RentalTerm_edit" dataProvider="{acRentalTerms}" labelField="label"/>
 
 
[Bindable] private var acRentalItem:ArrayCollection;
[Bindable] private var acRentalTerms:ArrayCollection;
 
Init Func: {
var _dcb:DealCB = new DealCB();
acRentalTerms = new ArrayCollection(_dcb.combo_RentalTerms);
}
 
private function getDealRentalItem_result(event:ResultEvent):void {
acRentalItem = event.result as ArrayCollection;
			
for (var i:int = 0; i < cb_RentalTerm_edit.dataProvider.length; i++) {
if (acRentalItem.getItemAt(0).RentalTerm == cb_RentalTerm_edit.dataProvider[i].label) {
cb_RentalTerm_edit.selectedIndex = i;
break; }
}
this.do_Nav("Edit");
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
private function getDealRentalItem_result(event:ResultEvent):void {
acRentalItem = event.result as ArrayCollection;
                       
for (var i:int = 0; i < cb_RentalTerm_edit.dataProvider.length; i++) {
if (acRentalItem.getItemAt(0).RentalTerm ...



Are you sure you're getting a event.result with at least one item in the arraycollection?
If you have an empty result getItemAt(0).RentalTerm will throw a null reference exception.
I've just mocked an example based on your code which works as long as event.result holds an arraycollection or more than 0 items.

Author

Commented:
I will look into that, but I believe I do.

I'm populating an edit form from a single record returned from a cfc, and all other fields are populating. I've made sure that each record contains a value, and the field names are correct. Is it possible that I need to strip for empty spaces (although there should be no extra spaces).

Author

Commented:
mplord, yes, there is always an event.result with one record.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial