11 Replies Latest reply on Aug 26, 2011 10:05 AM by Kristin95762

    help with undefined method

    Kristin95762 Level 1

      I'm new to class files, so I need a basic example/explanation.

       

      I have a main.mxml file, and my class simply loads a list upon startup.

       

      Now I want to change state with a radiobuttongroup so that my list will display optional data. I understand the basics of the radioButtonGroupand how it works.

       

      How do I instantiate the radioButtonGroup so that I can use it?

       

      This is what the code in the main.mxml looks like:

       

       

      <![CDATA[

      import classes.OpenDBAsync;

       

      import mx.controls.Alert;

       

      private var myDB:File;

       

       

      [Bindable]

      private var myDBclass:OpenDBAsync;

       

       

      private function init():void

      {

      myDBclass = new OpenDBAsync();

      }

       

       

      protected function modeSelectRBG_changeHandler(event:Event):void

      {

           if (modeSelectRBG.selectedValue == "authorState")

      {

           OpenDBAsync.authorStateLoadAuthors();

      } else {  

       

       

           OpenDBAsync.titleStateLoadTitles();

      }

      }

       

      ]]>

       

       

      I'm getting the error 1061: Call to a possibly undefined method %s through a reference with static type Class.

       

      Thanks

       

      Kristin

        • 1. Re: help with undefined method
          miguel8312 Level 3

          in order to instantiate a class you must

           

          your question:How do I instantiate the radioButtonGroup so that I can use it?

           

          private var localvar:radioButtonGroup  = new  radioButtonGroup;

           

           

          miguel

          • 2. Re: help with undefined method
            kokorito Level 4

            you need to use the instance name to call the class methods

             

            myDBclass.authorStateLoadAuthors();

             

            and

             

            myDBclass.titleStateLoadTitles();

             

             

            you can only use the class name when calling static methods

            • 3. Re: help with undefined method
              Kristin95762 Level 1

              Hey, thanks again for your input.

               

              The code in my mxml file looked just like what you posted here. However, what I didn't know was that I was leaving out the term 'static' in my class file method.

               

              In other words, and you probably know this anyway, but for future reference, my method should have looked like this:

               

              static public function authorStateLoadAuthors():void

              {

              trace("authors is selected");

              }

               

              Can you explain to me why this is a static method? I'd appreciate it.

               

              Thanks

               

              Kristin

              • 4. Re: help with undefined method
                kokorito Level 4

                a static method belongs to the class rather than the instance of a class so you dont need to instatiate it

                 

                sounds like it wont be long before you get to the singleton solution

                 

                 

                in mxml you declare a radiobuttongroup in the <Declarations> section (its non visual), or as miguel said in AS

                • 5. Re: help with undefined method
                  Kristin95762 Level 1

                  I don't really understand design patterns yet.

                   

                  But, I've figured out that my use of 'static' was not the solution. I mean, it worked when the method just launched a trace statement. But now I want to issue a new sqlstatement based on the radio button selected, and it's producing no results.

                   

                  So, What next?

                   

                  If I could just get the nuances of using class files down I'd be happy.

                   

                  Thanks

                   

                  Kristin

                   

                  PS: how do you put up with us noobs?

                  • 6. Re: help with undefined method
                    kokorito Level 4

                    LOL, because I am one

                     

                    well I'm a certified expert but I'm only just reading about design patterns, although the singleton is way the easiest and already been using it for the same project you are doing. How to have a database class that is available everywhere and can dispatch events.

                     

                    I havent got my head around any of the other patterns yet. I need to figure out MVC, I have a vague idea what that is, it apparently uses several patterns.

                    And then theres MVP, Model View Projector. Err, what?

                     

                    And then theres frameworks like robotlegs and swiz. Inversion of control. Sounds great, really geeky, but have no idea what it means. (yet)

                     

                    Yep, I'm a newb. Seems like the more you get into this the more you realise how big a newb you are

                    • 7. Re: help with undefined method
                      Kristin95762 Level 1

                      Wow. You have such a presence on the forums here. I just figured you were a Flex pro.

                       

                      So, do you have some idea how I would run another sqlstatement based on clicking a radio button? Because I got nothin'.

                       

                      I've tried passing in a file reference via the arguments, but that doesn't seem to do the trick.

                       

                      Thanks for sharing your discoveries.

                       

                      Kristin

                      • 8. Re: help with undefined method
                        miguel8312 Level 3

                        May i suggest adding a listener to the radiobutton and listening to the click event? once you listen to the click event you can make another call to the db.

                        Miguel

                        • 9. Re: help with undefined method
                          Kristin95762 Level 1

                          Hi

                           

                          Ya know how you look at something so hard and so long that you don't see what's going on? I don't know if this is it...but it may be.

                           

                          I have not defined my radio button in this fashion:

                           

                          <s:RadioButton id="authorRB"

                             label="List by author"

                             selected="true"

                             value="authorState"

                            click="someFunction()"/>

                           

                          It is just thus:

                           

                          <s:RadioButton id="authorRB"

                             label="List by author"

                             selected="true"

                             value="authorState"/>

                           

                          Then I have code in the <fx:Script> block

                           

                          private function titleRBOnClick(event:MouseEvent):void

                          {

                               myDBclass.titleStateLoadAuthors();

                          }

                           

                          Therefore, I don't have an eventListener. Are you suggesting I change my modus operandi?

                           

                          In my myDBclass.titleStateLoadAuthors(), I just have a trace function to see if I can get to that function. But when I change the code to run a sqlstatement, I get nothing. There's no data in the list.

                           

                          Just to ellucidate:

                           

                          My app opens up in State1 with the authorsList preloaded with author names. I have 2 radio buttons that change state between authors view and titles view. State2 has the titlesList in which I want to load the book titles.

                           

                          I figure I'm not reading the database file. I receive no errors.

                           

                          It's the class file that I'm having a hard time using. This all works fine as long as all my code is in the mxml file script block. I'm trying to organize my code better by using a class file, but it's got me all screwed up.

                           

                          Thanks

                           

                          Kristin

                          • 10. Re: help with undefined method
                            miguel8312 Level 3

                            kristin

                            I posted and example on my server.

                            Just go here

                            http://miguelontheweb.com/kristentestcase/testingStaticClass.html

                            right click view source.

                            and download the project locally.

                            once you do that look at the different ways to call the class with the raido button.

                            static methods and non static methods

                            Miguel

                            1 person found this helpful
                            • 11. Re: help with undefined method
                              Kristin95762 Level 1

                              I think you very much for putting that example together for me.

                               

                              For the sake of clarity, I'm going to include my code here so you can see what I'm doing.

                               

                              main.mxml file: I purposely left out unnecessary stuff to cut down on reading. This is for the desktop however.

                               

                              <s:states>

                                   <s:State name="authorState"/>

                                   <s:State name="titleState"/>

                              </s:states>

                               

                               

                              <fx:Script>

                                   <![CDATA[

                                        import classes.OpenDBAsync;

                               

                                        import mx.controls.Alert;

                               

                                        private var myDB:File;

                               

                                        [Bindable]

                                        private var myDBclass:OpenDBAsync;

                               

                                        private function init():void

                                        {

                                             myDBclass = new OpenDBAsync();

                                             myDB = myDBclass.db;

                               

                                             authorRB.addEventListener(MouseEvent.CLICK, authorRBOnClick);

                                             titleRB.addEventListener(MouseEvent.CLICK, titleRBOnClick);

                                        }

                               

                                        private function authorRBOnClick(event:MouseEvent):void

                                        {

                                             myDBclass.authorStateLoadAuthors();

                                        }

                               

                                        private function titleRBOnClick(event:MouseEvent):void

                                        {

                                             myDBclass.titleStateLoadTitles();

                                        }

                               

                                   ]]>

                              </fx:Script>

                               

                               

                              <s:List id="authorsList"

                              includeIn="authorState"

                              x="33" y="138" width="224" height="269"

                              dataProvider="{myDBclass.myAuthorResultAC}"

                              labelField="authorName"/>

                               

                               

                              <s:List id="titlesList"

                              includeIn="titleState"

                              x="36" y="118" width="344" height="279"

                              dataProvider="{myDBclass.myTitleResultAC}"

                              labelField="title"/>

                               

                               

                              <s:HGroup x="36" y="92" horizontalAlign="left" verticalAlign="top">

                              <s:RadioButton id="authorRB"

                                 label="List by author"

                                 selected="true"

                                 value="authorState"/>

                               

                               

                              <s:RadioButton id="titleRB"

                                 label="List by title"

                                 value="titleState"

                                 x.authorState="136" y.authorState="92"/>

                              </s:HGroup>

                               

                               

                              Class file: I'm removing the imports here to try to keep the confusion down. I don't want to get all tied up with getters/setters right now either. KISS is my motto.

                               

                              package classes

                              {

                                   ...imports...

                               

                               

                               

                              public class OpenDBAsync

                              {

                                   public var db:File;

                               

                                   private var selectAuthorsStm:SQLStatement;

                                   private var selectTitlesStm:SQLStatement;

                               

                                   private var conn:SQLConnection = new SQLConnection();

                               

                               

                                   [Bindable]

                                   public var myAuthorResultAC:ArrayCollection;

                               

                               

                                   [Bindable]

                                   public var myTitleResultAC:ArrayCollection;

                               

                               

                              public function OpenDBAsync()

                              {

                                   db = File.desktopDirectory.resolvePath("tarot.db");

                               

                                   conn.openAsync(db);

                               

                                   conn.addEventListener(SQLEvent.OPEN, selectData);

                              }

                               

                              private function selectData(event:SQLEvent):void

                              {

                                   selectAuthorsStm = new SQLStatement();

                                   selectAuthorsStm.sqlConnection = conn;

                               

                                   var sql:String = "SELECT * FROM authors3 ORDER BY authorName";

                                   selectAuthorsStm.text = sql;

                               

                                   selectAuthorsStm.addEventListener(SQLEvent.RESULT, onSelectResult);

                               

                                   selectAuthorsStm.execute();

                              }

                               

                              private function onSelectResult(event:SQLEvent):void

                              {

                                   var data:Array = selectAuthorsStm.getResult().data;

                                   myAuthorResultAC = new ArrayCollection(data);

                              }

                               

                              public function authorStateLoadAuthors():void

                              {

                                   trace("authors is selected");

                              }

                               

                              public function titleStateLoadTitles():void

                              {

                                   selectTitlesStm = new SQLStatement();

                                   selectTitlesStm.sqlConnection = conn;

                               

                                   var sql:String = "SELECT * FROM books2 ORDER BY title";

                                   selectTitlesStm.text = sql;

                               

                               

                                   //selectTitlesStm.addEventListener(SQLErrorEvent.ERROR, error);

                                   selectTitlesStm.addEventListener(SQLEvent.RESULT, loadTitlesResult);

                               

                               

                                   selectTitlesStm.execute();

                               

                               

                               

                                   trace("titles is selected");

                              }

                               

                              private function loadTitlesResult(event:SQLEvent):void

                              {

                                   var data:Array = selectTitlesStm.getResult().data;

                                   myTitleResultAC = new ArrayCollection(data);

                              }

                               

                              }

                              }

                               

                              When I select the titleRB, I'm changing state. 'titleState' contains a different list called titlesList in which I want to display titles upon loading. If you look at public function titleStateLoadTitles() the only thing that runs is the trace function. The dataprovider (myTitleResultAC) is empty.

                               

                              So, I figure I've lost connection to the database due to the way the code flows. This is not about static vs. nonstatic variables. Originally it was because I was trying to establish a connection with my class. A static function was not the solution because the function didn't seem to like my sqlstatement. Though it did help me begin to see what was going on.

                               

                              I hope this helps you see what I'm talking about.

                               

                              Thanks for your input. I've seen your website many times in my searches.

                               

                              Kristin