18 Replies Latest reply on Sep 25, 2012 6:41 AM by -BoNzO-

    List Error:invalidIndex when remove an item


      Hi Guys,


      I got the following exception, when i removed an item from spark list. I stepped into to see the index, i looked all right. Somehow, it went the following stack, and the index became an invalid index, which i am not sure where it was from. I found this thread - http://forums.adobe.com/thread/692482?tstart=0, but it is not the same problem.



      Error: invalidIndex
          at spark.layouts.supportClasses::LinearLayoutVector/remove()[E:\dev\4.x\frameworks\projects\ spark\src\spark\layouts\supportClasses\LinearLayoutVector.as:541]
          at spark.layouts::VerticalLayout/elementRemoved()[E:\dev\4.x\frameworks\projects\spark\src\s park\layouts\VerticalLayout.as:1311]
          at spark.components::DataGroup/http://www.adobe.com/2006/flex/mx/internal::itemRemoved()[E:\dev\4.x\frameworks\projects\s park\src\spark\components\DataGroup.as:1580]
          at spark.components::DataGroup/removeRendererAt()[E:\dev\4.x\frameworks\projects\spark\src\s park\components\DataGroup.as:625]
          at spark.components::DataGroup/removeAllItemRenderers()[E:\dev\4.x\frameworks\projects\spark \src\spark\components\DataGroup.as:656]
          at spark.components::DataGroup/createItemRenderers()[E:\dev\4.x\frameworks\projects\spark\sr c\spark\components\DataGroup.as:840]
          at spark.components::DataGroup/commitProperties()[E:\dev\4.x\frameworks\projects\spark\src\s park\components\DataGroup.as:899]
          at mx.core::UIComponent/validateProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx \core\UIComponent.as:7933]
          at mx.managers::LayoutManager/validateProperties()[E:\dev\4.x\frameworks\projects\framework\ src\mx\managers\LayoutManager.as:572]
          at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.x\frameworks\projects\framewo rk\src\mx\managers\LayoutManager.as:730]
          at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.x\frameworks\projects \framework\src\mx\managers\LayoutManager.as:1072]



      Thanks in advance,


        • 1. Re: List Error:invalidIndex when remove an item
          Shongrunden Adobe Employee

          Can you please provide a small sample application that demonstrates the error?

          • 2. Re: List Error:invalidIndex when remove an item
            cheng.wei Level 1

            Hi Shongrunden,


            Thanks for your reply. I tried to create a small sample application, but i could not reproduce the problem. Not sure what is the difference from the one of my application. I can't find any clue from the error stack, as it looks to me unrelated to the application code. Could you suggest a possible way to find the cause of the problem by the error stack?


            Thanks a lot,

            Cheng Wei

            • 3. Re: List Error:invalidIndex when remove an item
              cheng.wei Level 1

              When i debug i saw the variable value from DataGroup.as looks suspicious:

              -line 598

                   *  @private
                   *  Apply itemRemoved() to the renderer and dataProvider item at index.
                  private function removeRendererAt(index:int):void
                      // TODO (rfrishbe): we can't key off of the oldDataProvider for
                      // the item because it might not be there anymore (for instance,
                      // in a dataProvider reset where the new data is loaded into
                      // the dataProvider--the dataProvider doesn't actually change,
                      // but we still need to clean up).
                      // Because of this, we are assuming the item is either:
                      //   1.  The data property if the item implements IDataRenderer
                      //       and there is an itemRenderer or itemRendererFunction
                      //   2.  The item itself
                      // Probably could fix above by also storing indexToData[], but that doesn't
                      // seem worth it.  Sending in the wrong item here doesn't result in a big error...
                      // just the event with have the wrong item associated with it

                     //renderer is null, indexToRenderer is empty array with length of 4.
                      const renderer:IVisualElement = indexToRenderer[index] as IVisualElement;              
                      var item:Object;
                      if (renderer is IDataRenderer && (itemRenderer != null || itemRendererFunction != null))
                          item = IDataRenderer(renderer).data;
                          item = renderer;

                     //item is null, index is 3.
                      itemRemoved(item, index);                                                                                      

              -line 627

              Not sure if this gives any clue.


              Many thanks,

              Cheng Wei

              • 4. Re: List Error:invalidIndex when remove an item
                Shongrunden Adobe Employee

                How are you removing the item, do you call myList.dataProvider.removeItemAt(x) ?

                • 5. Re: List Error:invalidIndex when remove an item



                  I have exactly the same problem, getting the same error message and stack report (precisely the same).


                  All I can say about the error is that it is very random - I can't figure out what changes the situation to produce the error.


                  I have a class, extending the spark's TabBar, with a function to close a tab:


                  public class VerticalIconTabBar extends TabBar
                           public function closeTab(closedTab:int, selectedTab:int):void {
                               if (dataProvider.length == 0) return;
                               if (dataProvider is ViewStack){
                                   //remove the entire child from the dataProvider, which also removes it from the ViewStack
                                   (dataProvider as ViewStack).removeChildAt(closedTab);
                               //adjust selectedIndex appropriately
                               if (dataProvider.length == 0) {
                                   selectedIndex = -1;
                               } else if (closedTab < selectedTab) {
                                   selectedIndex = selectedTab - 1;
                               } else if (closedTab == selectedTab) {
                                   selectedIndex = (selectedTab == 0 ? 0 : selectedTab - 1);
                               } else {
                                   selectedIndex = selectedTab;



                  I am showing you the whole  function, though I believe that the '//adjust selectedIndex  appropriately' part is not behind the problem. The problem occurs,  though, only when I am removing the last element from the taskbar (as far as I can tell) and it happens after this function has been called.


                  One thing I noticed, but it might be sort of a red herring, is that I can reproduce the error after working a little bit longer (20+ seconds lets say) in the particular Viewstack view - or it's not about time but rather about clicking on different buttons in my UI, I am simply guessing.


                  These are the locals of the LinearLayoutVector and its remove(index:uint) function at the time of the error thrown:


                  this: pendingInserts = null, pendingRemoves = null, pendingLength = -1, length = 0


                  index = 0 (this causes the error as it equals this.length)

                  lastIndex = 0

                  intervalStart = 0


                  I suppose the length shouldn't be 0 or the function shouldn't be called at all - but I cannot affect that.


                  I would be very grateful if you could point me towards the direction for solving this, as it causes the whole UI to jam and the only solution is to restart the whole application.


                  Thank you,


                  • 6. Re: List Error:invalidIndex when remove an item
                    Shongrunden Adobe Employee

                    If you set useVirtualLayout="false" on the TabBar do you get other errors?


                    Can you create a small, complete sample application that demonstrates this? 

                    • 7. Re: List Error:invalidIndex when remove an item
                      xixixao Level 1

                      That's exactly the type of idea I needed - it made me look at the DataGroup (inside of the false stack) and voila - the problem was in a Spark List I am also using inside of the application! (it would probably be enough to read the originial post - well, silly me)


                      Now I know precisely when the error happens:

                      <s:List labelFunction="formatSelected" dataProvider="{dp.selected}" />


                      This is the list, and as you could guess, "dp" is an object, that changes with the controlling of the TabBar.


                      1) If I set dp to null and prior to that, didn't change dp.selected from its initial new ArrayCollection(), then the error wasn't thrown.


                      2) If I set dp to null, and prior to that, did add some objects to the ArrayCollection, everything was still fine.


                      3) But if set dp to null, and prior to that changed the ArrayCollection and as the last step called ArrayCollection.removeAll(), then the error did occur.


                      Now, I have a feeling that the problem might be inside of the binding mechanism of MXML (which by the way doesn't work as expected:

                      <info:MultipleLinesBox showedArray="{dp.selected}"> -> triggers only with a change to "dp" or assignment to "dp.selected", but not when you add or remove items - as it doest with Spark List's 'dataProvider' - another question of mine )


                      Two interesting facts are:

                      1) setting useVirtualLayout="false" on the Spark List does solve the problem

                      2) if so, in a situation where the error occured previously, now the list "shrinks" to almost 0 height (the border stays, probably)


                      I won't be probably using the List at all in the final product (another 'by the way': the fact that I have to go and search for and in the ListSkin.mxml file to change the number of List's required rows properly in my application still seems ridiculous to me - but that's Flex4's skins in all their beauty ) but I am still interested in the whereabouts of this error...

                      • 8. Re: List Error:invalidIndex when remove an item
                        Denis Elkin

                        Hello all,


                        I have exactly the same error stack.

                        The error was extremely hard to reproduce because I have several asyncronious calls that update the List. But I've catched this error several times but cannot debug it because it is very rare.


                        Does anybody know the good workaround?

                        • 9. Re: List Error:invalidIndex when remove an item
                          xixixao Level 1

                          As I stated above:


                          1) setting useVirtualLayout="false" on the Spark List does solve the problem

                          You might want to try it first (unless you need VirtualLayout to be 'true').

                          • 10. Re: List Error:invalidIndex when remove an item
                            cheng.wei Level 1

                            Thanks a lot.



                            Cheng Wei

                            • 11. Re: List Error:invalidIndex when remove an item
                              Denis Elkin Level 1

                              Thank you!


                              I will try to set useVirtualLayout to false and check if it helps.

                              • 12. Re: List Error:invalidIndex when remove an item
                                Denis Elkin Level 1

                                I performed some tests. Unfortunately I cannot set useVirtualLayout="false" due to perfomance reasons. The application becomes unusable after adding 50+ elements to the List (the ItemRenderer is rather complex).


                                The question to Adobe support:

                                Have you reproduced the problem?

                                Is it true that this is a bug in Flex SDK?

                                If it is true, when do you plan to create a bugfix?


                                This is a very important issue for me.

                                Great thanks in advance for your support!

                                • 13. Re: List Error:invalidIndex when remove an item
                                  Shongrunden Adobe Employee

                                  No we haven't reproduced the problem yet.  In order to investigate further we need a sample application that demonstrates the issue.


                                  If anyone can take their existing project, duplicate it, then strip out as much uneccessary code as possible while still reproducing the bug, then we can investigate further. 


                                  I'd be happy to help with this "stripping down" process if someone can send me a complete, compilable FXP to start from.

                                  • 14. Re: List Error:invalidIndex when remove an item
                                    xixixao Level 1

                                    I'll try to make the sample, but it won't be before the next weekend.

                                    • 15. Re: List Error:invalidIndex when remove an item
                                      John Hall Level 4

                                      I'm having the exact same problem now so I'll be watching this thread. Just discovered it. I see in a question above it is asked if we're removing the item from the dataProvider of the list or the source arrayCollection (I'm doing the latter). Setting useVirtualLayout = "false" doesn't help.

                                      • 16. Re: List Error:invalidIndex when remove an item
                                        Denis Elkin Level 1

                                        I've noticed one more thing. This issue occurs only on last Flash Player (10,1,102,64). On FP version 10.0 the player did not crash on this issue.

                                        Hope this helps.

                                        • 17. Re: List Error:invalidIndex when remove an item

                                          Did you ever get this fixed?


                                          I'm managing to reproduce it 18 months later....


                                          Latest Flex SDK and latest Air SDK. Practically the same issue you're having. It occurs for me when I call a removeAll() on the array collection that is bound as the data provider for a spark list.

                                          • 18. Re: List Error:invalidIndex when remove an item

                                            This problem comes from DataGroup trying to DELETE an Array property (Let's hope from a crappy refactoring): 


                                            // Remove previously "in view" IR from the item=>IR table

                                            var elt:IVisualElement = indexToRenderer[vrIndex] as IVisualElement;

                                            delete indexToRenderer[vrIndex];


                                            It kind of remove the item at vrIndex... But leaves the Array in a terrible state (e.g the length property remains at the current value).


                                            This is beyond broken, even in the super permissive JS it wouldn't work. By the way, time to move to JS :-P