13 Replies Latest reply on May 31, 2011 8:26 AM by ministe

    why are buttons Null after several button clicks?

    ministe

      Hi all,

      I'm starting to learn flex and have set up some component pages, a custom event actionscript and all I want to do is have button clicks change which page I'm on.  I have a ViewStack which is made up of my pages, like so:

      <mx:ViewStack id="viewstack" includeIn="Running" width="100%" height="100%">
           <views:HomePage id="HomePage"/>
           <views:ProductPage id="ProductPage"/>
           <views:OrderListPage id="OrderListPage"/>
      </mx:ViewStack>
      
      

       

      my views are pages made from <s:NavigatorContent>.  They each contain a BorderContainer component which will contain the page data, and a reference to a buttonBar component which is simply the buttons, like so:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:NavigatorContent  xmlns:fx="http://ns.adobe.com/mxml/2009" 
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:components="components.*" xmlns:model="model.*">     
           <s:width>100%</s:width>
           <s:height>100%</s:height>
           <s:layout>
                <s:VerticalLayout/>
           </s:layout>
      
           <components:ButtonBar />
           
           <s:BorderContainer>
                <s:width>100%</s:width>
                <s:height>100%</s:height>
                <mx:Text text="this is the home page" />
           </s:BorderContainer>
      </s:NavigatorContent>
      

       

      and the button bar:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:ButtonBar xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx"
                change="changeHandler(event)">
           
           <fx:Script>
                <![CDATA[
                     import events.StateEvent;
                     import spark.events.IndexChangeEvent;
                     
                     private function changeHandler(event:IndexChangeEvent):void{
                          var stateEvent:StateEvent;
                          switch(event.target.selectedItem){
                          case "Home":
                               stateEvent = new StateEvent(StateEvent.CHANGE_STATE, StateEvent.HOMEPAGE, true);
                               break;
                          case "Products":
                               stateEvent = new StateEvent(StateEvent.CHANGE_STATE, StateEvent.PRODPAGE, true);
                               break;
                          case "My Orders":
                               stateEvent = new StateEvent(StateEvent.CHANGE_STATE, StateEvent.ORDERPAGE, true);
                               break;
                          }
                     this.dispatchEvent(stateEvent);
                     }
                ]]>
      
                </fx:Script>
                <s:x>0</s:x>
                <s:y>0</s:y>
                <s:width>100%</s:width>
                <s:ArrayList source="[Home, Products, My Orders]" />
      </s:ButtonBar>
      
      

       

      The event bubbles to the application with a string which I use to set the viewstate:

      private function stateManager(event:StateEvent):void{
           if(viewstack.selectedChild.name != event.state){
                viewstack.selectedChild=NavigatorContent(viewstack.getChildByName(event.state));
           }
      }
      

       

      I can click the buttons OK a few times then after several clicks, I get this error:

      Main Thread (Suspended: TypeError: Error #1009: Cannot access a property or method of a null object reference.)

       

      It seems sporadic and random, I cant find a pattern.  There's only the 3 buttons and they all work fine until after a few clicks when I get that.  I cant get past it.  Does anyone know what I'm doing wrong?  I'm very new at Flex so I'd appreciate any advice at all.

       

      Thanks

      Steven

        • 1. Re: why are buttons Null after several button clicks?
          Flex harUI Adobe Employee

          Hard to say.  Post the entire error including the stack dump.

           

          FWIW, getChildByName is a low-level Flash API and may not work as expected

          in a Flex environment.

          • 2. Re: why are buttons Null after several button clicks?
            ministe Level 1

            Thanks for your reply

             

            Console:

            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf\[[DYNAMIC]]\1 - 370,463 bytes after decompression
            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf\[[DYNAMIC]]\2 - 2,311,734 bytes after decompression
            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf - 317,840 bytes after decompression
            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf\[[DYNAMIC]]\3 - 650,777 bytes after decompression
            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf\[[DYNAMIC]]\4 - 441,362 bytes after decompression
            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf\[[DYNAMIC]]\5 - 1,432,382 bytes after decompression
            [SWF] F:\Eclipse\Workspace - Flex\Woe2\bin-debug\Woe2.swf\[[DYNAMIC]]\6 - 323,354 bytes after decompression
            products    \
            my orders    > these 3 are trace output from the successful button clicks
            home        /
            TypeError: Error #1009: Cannot access a property or method of a null object reference.
                 at flash.events::EventDispatcher/dispatchEvent()
                 at mx.core::UIComponent/dispatchEvent()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:12528]
                 at components::ButtonBar/changeHandler()[F:\Eclipse\Workspace - Flex\Woe2\src\components\ButtonBar.mxml:36]
                 at components::ButtonBar/___ButtonBar_ButtonBar1_change()[F:\Eclipse\Workspace - Flex\Woe2\src\components\ButtonBar.mxml:5]
                 at flash.events::EventDispatcher/dispatchEventFunction()
                 at flash.events::EventDispatcher/dispatchEvent()
                 at mx.core::UIComponent/dispatchEvent()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:12528]
                 at spark.components.supportClasses::ListBase/commitSelection()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\supportClasses\ListBase.as:1158]
                 at spark.components.supportClasses::ListBase/commitProperties()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\supportClasses\ListBase.as:830]
                 at spark.components.supportClasses::ButtonBarBase/commitProperties()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\supportClasses\ButtonBarBase.as:261]
                 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\framework\src\mx\managers\LayoutManager.as:730]
                 at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1072]
            
            

             

             

            Debug:

            file:/F:/Eclipse/Workspace%20-%20Flex/Woe2/bin-debug/Woe2.html     
                 Main Thread (Suspended: TypeError: Error #1009: Cannot access a property or method of a null object reference.)     
                      flash.events::EventDispatcher/dispatchEvent [no source]     
                      mx.core::UIComponent/dispatchEvent     
                      components::ButtonBar/changeHandler     
                      components::ButtonBar/___ButtonBar_ButtonBar1_change     
                      flash.events::EventDispatcher/dispatchEventFunction [no source]     
                      flash.events::EventDispatcher/dispatchEvent [no source]     
                      mx.core::UIComponent/dispatchEvent     
                      spark.components.supportClasses::ListBase/commitSelection     
                      spark.components.supportClasses::ListBase/commitProperties     
                      spark.components.supportClasses::ButtonBarBase/commitProperties     
                      mx.core::UIComponent/validateProperties     
                      mx.managers::LayoutManager/validateProperties     
                      mx.managers::LayoutManager/doPhasedInstantiation     
                      mx.managers::LayoutManager/doPhasedInstantiationCallback     
            file:/F:/Eclipse/Workspace%20-%20Flex/Woe2/bin-debug/Woe2.html     
            
            

             

             

            This time I started on the home page and clicked Products then Orders then Home then Products and as you can see from the traces, it dies after clicking products for the 2nd time.

             

            I initially wasn't using getChildByName, I assumed I could just use the String that I'm passing from the event and set the selectedName based on that, since it matches an ID.  To me, that's how it appears to work in the Flex 4 training book, but it dies for me

             

            Thanks!

            • 3. Re: why are buttons Null after several button clicks?
              Flex harUI Adobe Employee

              I would put some defense in your switch statement so you don't dispatch a

              null event and see what selectedItem is when it doesn't match one of the

              cases

              • 4. Re: why are buttons Null after several button clicks?
                Level 2

                http://activeden.net/item/membership-sytem/123720  Probably worth the $6  I bot it a month ago ... loads nicely into fb4 and 4.5  Performs your viewstack and navig and separate files per page

                • 5. Re: why are buttons Null after several button clicks?
                  Level 2

                  Flex HarUI raises a good point, perhaps related to this example:

                   

                  http://help.adobe.com/en_US/flex/using/WSc2368ca491e3ff92-59bf082612135c9e688-8000.html

                   

                  look at the last running example and its code right above the comments. It uses a ButtonBar and a change handler. When you click between different buttons it works fine showing the index and button name. When you click on a button that is the last one clicked it returns index of -1 and undefined. Might be related to your issue. The example 2 above it works fine as this index info is not being shown and you would already be sitting on its Nav content but not forced to shown a blown index and undefined label. so I think this might be your issue.

                  • 6. Re: why are buttons Null after several button clicks?
                    ministe Level 1

                    I have previously put some defence in my switch, but while it stops the application from crashing, it still gives me the problem where some button clicks do not casuse the page to change.  Here's some trace output:

                     

                    index: 1
                    label: Products
                    index: 2
                    label: My Orders
                    index: 0
                    label: Home
                    index: -1
                    label: undefined
                    BUTTON CLICK FAILED
                    index: 2
                    label: My Orders
                    index: 2
                    label: My Orders
                    

                     

                    As you can see I start on Home and then go to Products, Orders, Home and then it fails when I try to click products again.  In response to the link provided by dpierce747 I was able to click the Orders button twice and both times received the correct index and label, not -1.  So clicking on the same button doesnt seem to be an issue.  Here's another stack of button clicks:

                     

                     

                    index: 2
                    label: My Orders
                    index: 0
                    label: Home
                    index: 0
                    label: Home
                    index: -1
                    label: undefined
                    BUTTON CLICK FAILED
                    index: 2
                    label: My Orders
                    index: 1
                    label: Products
                    index: 2
                    label: My Orders
                    index: -1
                    label: undefined
                    BUTTON CLICK FAILED
                    

                     

                    Seems sporadic still and I cant nail it down to fix it.  I assumed I was doing something wrong that was easy and simple because I'm new to Flex, that seems to not be the case!

                     

                    ADD:

                     

                    I've added some more stuff to the trace, I'm outputting the event phase, old index, new index and selected item.  here is some results:

                     

                     

                    ----------
                     event phase: 2
                     old index: -1
                     new index: 1
                     label: Products
                    ----------
                     event phase: 2
                     old index: -1
                     new index: 2
                     label: My Orders
                    ----------
                     event phase: 2
                     old index: -1
                     new index: 0
                     label: Home
                    ----------
                     event phase: 2
                     old index: 1
                     new index: -1
                     label: undefined
                    BUTTON CLICK FAILED
                    ----------
                     event phase: 2
                     old index: -1
                     new index: 1
                     label: Products
                    
                    ----------
                    

                     

                    Event phase is always 2 (AT_TARGET) so that's fine.

                    Strangely, old index is always -1.  I would've thought that should match the index of the currently selected view shouldn't it?  The biggest thing is, look at the old index when the event fails... it's not minus 1, it's 1!  I really hope someone can shed some light as to why this might be happening

                     

                    Thanks!

                    • 7. Re: why are buttons Null after several button clicks?
                      ministe Level 1

                      OK it seems the issue is to do with the fact that I have multiple instances of the ButtonBar in my current setup.  If I remove the buttonbar from the pages and put it in my application I no longer have the issue.  Trouble is, I dont WANT the button bar in my application, I want it on the pages!  So is there a way to get around this?

                       

                      Thanks

                      • 8. Re: why are buttons Null after several button clicks?
                        Level 2

                        i know, you want them in separate pages. i will look into that. can you zip it up and email it to me ?

                         

                        change it so it is not anything secretive.

                         

                        as for your second to last  post i am focusing on when newindex=-1 not oldindex=-1

                         

                        so here is my question? does it fail only when you click on the one that has the current focus (in which case who cares)

                         

                        or does it fail at times when you click on a button that was not previously the selection (that is bad)

                        • 9. Re: why are buttons Null after several button clicks?
                          ministe Level 1

                          As I mentioned before, I am able to click the button of the page currently in focus and it works fine.  I get the issue when clicking a button that should take me to a different page, ie not the currently selected page.

                           

                          I may be able to zip it up, I'll see what I can do.

                           

                          Thanks

                          Steven

                          • 10. Re: why are buttons Null after several button clicks?
                            Level 2

                            ok so that is bad.

                             

                            what are you looking to achieve with the buttons ?  a new set of horizontal buttons on each page perhaps, with a way to navigate back to the top ?

                             

                            Date: Mon, 16 May 2011 05:32:49 -0600

                            From: forums@adobe.com

                            To: drewpierce@live.com

                            Subject: why are buttons Null after several button clicks?

                             

                            As I mentioned before, I am able to click the button of the page currently in focus and it works fine.  I get the issue when clicking a button that should take me to a different page, ie not the currently selected page.

                             

                            I may be able to zip it up, I'll see what I can do.

                             

                            Thanks

                            Steven

                            >

                            • 11. Re: why are buttons Null after several button clicks?
                              ministe Level 1

                              At the moment the buttons just exist as a way to navigate between some pages on an order site.  Allowing the user to navigate between their order history, the home page and page where they can view products. I only want the buttons on these pages because after this they'll enter an order wizard so won't need these buttons.

                               

                              I suppose one method would be to have a button bar on the application and limit visibility to those pages, but I'd rather have them within the necessary pages if possible

                              • 12. Re: why are buttons Null after several button clicks?
                                Level 2

                                i have been unable to find a good example but have been hacking away with some code from

                                 

                                http://stackoverflow.com/questions/3678106/remove-child-content-from-viewstack-in-flex-4

                                 

                                that messes with the viewstack. so on individual pages, perhaps on an event like ???? you would know it was activated and change the menu. that way you main app mxml (or .as) won't have all those menu switches and you could encapsulate. but the whole thing is pretty lame.

                                 

                                you could call

                                 

                                FlexGlobals.topLevelApplication.vs.removeChild('newContent' ...   add child that kind of stuff. remove the menu then recreate it

                                 

                                where vs is you id for your viewstack

                                 

                                if you go back to

                                 

                                http://trumpboston.com/membership/Main.html

                                 

                                user:     drewpierce@live.com

                                password:  abcd

                                 

                                once you login the buttons change. there is only 1 viewstate change, that is when you logged in

                                 

                                well there are 2 states but there are not like 10 menus. you could do that. it would not blow up

                                 

                                the entire viewstate is baked into one application object with viewstates showing when each button may be visible

                                 

                                if you want the project send me you email address. don't forget to pay for it on activeden.  $6

                                http://activeden.net/item/membership-sytem/123720

                                 

                                main app:

                                http://pastie.org/1910085

                                 

                                login control to begin with that controls initial state:

                                http://pastie.org/1910088

                                 

                                proj pict you can see it has several pages or forms

                                proj_pict.JPG

                                • 13. Re: why are buttons Null after several button clicks?
                                  ministe Level 1

                                  This seems to be a great hassle, I can't believe its so bloody hard to have a button click work successfully unless the buttonbar is on the application instead of its pages!  Anyway, I've given up and put the button bar on the application and will render it conditionally.  Spoilt my architecture but its too much of a nightmare to fix.