Here is sample code (SpecifiedIndexError.mxml):
* To compile solely this Flex application from the command line, use
* mxmlc -debug=true -strict=true SpecifiedIndexError.mxml
* This application demonstrates one situation in which a relatively common
* RangeError can occur.
public function runDoNotTryThisAtHome():void
var form:Form = new Form();
// WARNING: The following line should be inside of the for loop and not
// outside of the loop as shown here. This code will compile,
// but will result in an error:
// RangeError: Error #2006: The supplied index is out of bounds.
// Move the instantiation of each FormItem down inside the loop
// makes the error go away and the application works properly.
var formItem:FormItem = new FormItem();
for ( const index:int = 0 ; index < 5; index++ )
formItem.label = "Label-" + index;
<mx:Label text="Watch for constructed form!" />
In the code in the example above has the two most interesting lines highlighted. The first line is the instantiation of the FormItem before the loop. This is a mistake because the FormItem should be instantiated inside the loop so that there is a different instance of FormItem each time through the loop. This code will compile as-is, but will have the RangeError at runtime when the same FormItem is attempted to be added to the Form multiple times. The second highlighted line shows the line of offending code as reported by the exception message shown below.
RangeError: Error #2006: The supplied index is out of bounds.
Moving the line of code that instantiates FormItem down into the for loop resolves the problem and the application runs correctly.
The main point of this entry is to show that RangeError problems can occur even outside of the traditional array context. As another blog entry shows, this error can also occur when moving a component from one parent container to another parent container.