1 Reply Latest reply on Jun 5, 2009 1:16 PM by tlaneadobe

    Adding element to dataprovider collection works but returns error

    Justin Schroeder

      I have a list control that is being populated from 2 mysql tables (see this thread: http://forums.adobe.com/message/2013092). Currently the following function is doing that work:

       

      protected function changeDataProvider(providerType:String):void{
           if(providerType == "subcategories"){
              var subcategoriesData:IList = TypeUtility.convertToCollection(subcategoriesResult.lastResult);
           // create the back button here:
           var backButton:Object = {name:"« Back", id:"main"}
                               
           //add it to the list of results here:
           subcategoriesData.addItemAt(backButton,0);
                               
           // add the type to all these functions
                               
           category.dataProvider = subcategoriesData;
           category.labelField = "name";
           }else if(providerType == "categories"){
              var categoryData:IList = TypeUtility.convertToCollection(categoriesResult.lastResult);
                               
           // add a cat_type to the object with "category" as the type
                               
           category.dataProvider = categoryData;
           category.labelField = "category_name";
           }
      }
      

       

      Like I said, this function IS working. The line I'm having trouble with is the where I add the "backButton" object I create:

      subcategoriesData.addItemAt(backButton,0);

      Actually, durring the runtime this also works just fine. But when I run it in debug mode, it always stops and returns this error on that line:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
           at groupbrowser/changeDataProvider()[/Volumes/htdocs/justin/flex/groupbrowser/src/groupbrowser.mxml:45]
           at groupbrowser/selectListItem()[/Volumes/htdocs/justin/flex/groupbrowser/src/groupbrowser.mxml:29]
           at groupbrowser/__category_doubleClick()[/Volumes/htdocs/justin/flex/groupbrowser/src/groupbrowser.mxml:68]
      

       

      Reading adobe's docs on error #1009 it actually says that this error may be returned with valid code (that sounds like poor error handling to me, doc found here http://livedocs.adobe.com/flex/201/langref/runtimeErrors.html). I'm just begining in Flex, and don't want to get into the habit of ignoring errors.

       

      How can I fix this code so it stops returning this error but maintains its current functionality? Thanks in advance.