10 Replies Latest reply on Jun 11, 2007 9:04 AM by SymTsb

    Undefined property

    hciguy Level 1
      Hey guys,

      I made a class called XMLParser. I then instantiate it like so:

      var appMenubar:xmlParser = new xmlParser("xml/menubar.xml");

      Then I try to access stuff inside of it:

      trace("Do we have it? " + appMenuBar.returnObject.menuitem1);

      The error I get is: "1120: Access of undefined property appMenuBar."

      Why can't I acess appMenuBar? What am I missing?

      Also, I made a public Object within the XMLParser class. Should I be able to access it directly once I instantiate an instance of the class?

      Thanks so much in advance!!!
        • 1. Re: Undefined property
          hciguy Level 1
          Actually I fixed that. Forgot stuff is case sensitive now...so here is my issue now. I have the code below and it is giving me this error: "1120: Access of undefined property nodes." I made a similar block of code which worked fine on its own but this block won't work within my class....any ideas?

          Two blocks attached...
          • 2. Re: Undefined property
            hciguy Level 1
            So apparently this "null object reference" means so to speak that the object is unavailable to be acted upon? What am I doing wrong? I have this right in my class: var nodes:Object = new Object; And then when I try to use it i get this error....I am missing something dumb I know it lol

            Here is AS class def's explaination:

            "An object that evaluates to null can have no properties. This error can occur in some unexpected (though valid) situations. For example, consider the following code, which creates a Sprite object. Because this Sprite object is never added to the display list (through the addChild() method of a DisplayObjectContainer object), its stage property is set to null. Thus, the example generates this error because Sprite object's stage property cannot have any properties:

            import flash.display.Sprite;
            var sprite1:Sprite = new Sprite();
            var q:String = sprite1.stage.quality;"

            Thanks!
            • 3. Undefined property
              SymTsb Level 2
              check where you are defining nodes. It should be inside the class declaration and before the class constructor. This is usually a scope issue. If you declared the variable inside the constructor, it may not be within the scope of the function you are now calling. Otherwise, I'd suggest pasting the first few lines before and after the declaration of variable online for us to see. Could be a plethora of things unrelated.

              Having seen your latest post, double check that code again for nodes....

              var nodes:Object = new Object(); (you omitted the () in your post so just making sure)

              If you don't already have it, add nodes to the display list for the class... when it is declared is fine. addChild( nodes ); See if that makes any difference.
              • 4. Re: Undefined property
                hciguy Level 1
                Here's the code...
                • 5. Undefined property
                  SymTsb Level 2
                  Where is the class constructor??? Right now, looking at this code, your class isn't instantiated so any calls to any of its functions is gonna have some unpleasant results.

                  I'd also recommend changing nodes to a private var as well as your other vars at the highest level in the class. Generally, you want these vars private and you write accessor functions (setters/getters) to handle them. This prevents unwanted changes to the vars later.

                  and check the Object declaration issue from my last post. The current code isn't proper declaration style when using "new".
                  • 6. Re: Undefined property
                    hciguy Level 1
                    I did forget the constructor for node...I just added it. Do I need to do anything in it? What difference does it make not having one?

                    I don't know about getter/setters. I'll look it up. Why do I need them?

                    This class nodes is to be a class to handle all of the adding/modifying/handling of nodes that my XMLParses class pulled from an XML document. I wanted the nodes Object to be available to my XML Parser object, if that makes sense, so the user can assess it from the main FLA. Is it wrong to just make it public? I guess so...wouldn't want them messing with it...so I use getters/setters...I'll look it up.

                    Sorry! I am just trying to get used to using classes and things so I am sure I am missing some of the aspects of it. I apologize for these foolish errors. But I am learning :)

                    THANK YOU SO MUCH FOR YOUR HELP! :)
                    • 7. Re: Undefined property
                      hciguy Level 1
                      Fyi...I added the constructor and I fixed the object syntax..still getting same "Cannot access a property or method of a null object reference."

                      I don't get it because if I do it by itself in an FLA, it works. Within my class it fails....argh.

                      Thanks!
                      • 8. Re: Undefined property
                        SymTsb Level 2
                        A class constructor is backbone of your class. Lets say you create that class in AS (as you have done).

                        public class myClass extends MovieClip{

                        You want to use that class right? So inside another Flash file you would declare a variable, type cast it properly and use new to instantiate it.

                        var _mco:MovieClip = new myClass();

                        Typically, when you instantiate a class, you want the bare essentials for that class to be predefined. Think of it as an insurance policy on your new BMW. You want to make sure everything is in order before you get behind the wheel of that car. That is the purpose of the constructor. So lets go back to that class we defined earlier.

                        public class myClass extends MovieClip{

                        private var _loadermc:MovieClip;
                        private var _myvar:Number;

                        public function myClass():void{
                        _loadermc = new MovieClip();
                        _myvar = 0;
                        addChild( _loadermc );
                        }
                        }

                        The constructors purpose is simply to set things in order for the class as you should see from my example. I made sure that _loadermc was instantiated as a Movie Clip and _myvar was set to 0 and then I added _loadermc to the display list for my class. In more in-depth applications, you are going to call builder functions (say to construct the elements of a user interface).

                        About accessor functions and private vs. public variables....

                        In most situations, it is better to create private variables. This means they are only accessible to the methods (functions) inside your class. This prevents unwanted tampering (say you decide to share your class/package with the world) during the codes execution. This means however that only your class can access those variables. To get around that, you can create what are called accessor functions (also known as setters and getters). They will either get or set the private variable for you without actually broadcasting information about that variable. Back to our class again....

                        Lets say we want people to be able to set the value of _myvar to anything they may need during the course of the myClass objects lifespan.

                        public class myClass extends MovieClip{

                        private var _loadermc:MovieClip;
                        private var _myvar:Number;

                        public function myClass():void{
                        _loadermc = new MovieClip();
                        _myvar = 0;
                        addChild( _loadermc );
                        }

                        public function setMyVar( _val:Number ):void{
                        _myvar = _val;
                        }

                        public function getMyVar( ):void{
                        return _myvar;
                        }
                        }

                        setMyVar and getMyVar do exactly as their names imply, they either set or get the value of the private variable _myvar.

                        If you need further assistance, please feel free to e-mail my - mike@sd-dezign.com I'm more then up to help you better understand the concepts. I'd also recommend putting yourself on the waiting list for Colin Moock's Essential Actionscriipt 3.0 (due out pretty soon I believe).
                        • 9. Re: Undefined property
                          hciguy Level 1
                          Okay so I figured out the problem and now I am back to square one...how to make store my data....I thought the code below worked but it doesn't...hopefully from it you can see what I am trying to do...? Need to make thisTest.stuff.otherstuff dynamically on the fly....somehow...
                          • 10. Re: Undefined property
                            SymTsb Level 2
                            If you can, e-mail me the FLA and class file. I'm about to head out on lunch. I can look at the file personally. This way, I can actually debug the error itself from within the environment. :D