5 Replies Latest reply: Aug 9, 2012 12:55 PM by Ned Murphy RSS

    Error: Cannot access a property or method of a null object reference. at enemy/loop()

    SeaMonster1311

      Hi everybody, i'm learning flash for a few days. I want to write a simple game.. We move a "gun", and if we click mouse, the gun send a bullet. Then if bullet hit the enemy, enemy and bullet disapear. Unfortunately, i have a error That's my fragment of code:

       

      // main (in game.fla):

      var bulletholder:MovieClip = new MovieClip();

      addChild(bulletholder);

       

      function shoot(e:Event):void

      {

                player.y = 400;

       

                var bul:MovieClip = new bullet();

                bul.x = player.x+21;

                bul.y = player.y-10;

                bul.width = 30;

                bul.height = 30;

                bulletholder.addChild(bul);

      }

       

       

      // this is code in enemy.as:

      public class enemy extends MovieClip {

       

                          private var core:Object;

       

       

                          public function enemy() {

                                    addEventListener(Event.ENTER_FRAME, loop);

                                    x = Math.random()*550;

                                    y = -100;

                                    core = MovieClip(root);

                          }

       

                          private function loop(e:Event) {

                                    y += 4;

       

                                    var ilosc:int = core.bulletholder.numChildren; // in this moment, debug show error !

       

                                    for(var i:int = 0; i < ilosc; i++)

                                    {

                                              var target:MovieClip = core.bulletholder.getChildAt(i);

       

                                              if(hitTestObject(target))

                                              {

                                                        core.bulletholder.getChildAt(i).remove(); // remove() is my function from bullet.as

                                                        core.bulletholder.removeChild(target);

       

                                                        removeEventListener(Event.ENTER_FRAME, loop);

                                                        core.removeChild(this);

                                              }

                                    }

       

                                    if(y > 600)

                                    {

                                              removeEventListener(Event.ENTER_FRAME, loop);

                                              core.removeChild(this);

                                    }

                          }

       

       

                }

       

      Please help me, I'm stuck here now for 2 days

       

      And sorry for any mistakes, i don't know english very well.

        • 1. Re: Error: Cannot access a property or method of a null object reference. at enemy/loop()
          Ned Murphy MVP

          You should include the complete error message in your posting.

           

          The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

           

          - is declared but not instantiated

          - doesn't have an instance name (or the instance name is mispelled)

          - does not exist in the frame where that code is trying to talk to it

          - is animated into place but is not assigned instance names in every keyframe for it

          - is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).

           

          If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

          • 2. Re: Error: Cannot access a property or method of a null object reference. at enemy/loop()
            SeaMonster1311 Community Member

            Okay, thanks

            I do this:

             

            // in class enemy:

            private var bulletholder:Object;

             

            // constructor this class:

            bulletholder = new Object();

             

            // and then:

            public function Set(bulle):void {

                                          bulletholder = bulle;

                                }

             

            // in the main file, i wrote this:

            function createEnemy(e:Event):void

            {

                      var en:enemy = new enemy();

                      en.Set(bulletholder);

                      addChild(en);

            }

            And works fine But, in this code:

            var ilosc:int = bulletholder.numChildren;

             

            for(var i:int = 0; i < ilosc; i++)

            {

                   var target:DisplayObject = bulletholder.getChildAt(i);

             

                                                    if(hitTestObject(target))

                                                    {

                                                              bulletholder.removeChild(target);

             

                                                              removeEventListener(Event.ENTER_FRAME, loop);

                                                              y = x = -100;

                                                    }

            }

            in this line:

             

            var target:DisplayObject = bulletholder.getChildAt(i);

             

            is error if i have two or more bullets on the screen.. If i have only 1 bullet, it's okay..

            RangeError: Error #2006: The supplied index is out of bounds.

                      at flash.display::DisplayObjectContainer/getChildAt()

                      at enemy/loop()[C:\Users\Jakub\Desktop\Flash\gierka1\enemy.as:36]

             

            It doesn't matter, game works fine, but what I can do, to delete this error?

            • 3. Re: Error: Cannot access a property or method of a null object reference. at enemy/loop()
              Ned Murphy MVP

              When you remove a child you reduce the number of children and all children above that index shift down to fill that gap.  Your loop counter "ilosc" is based on the number of children before you removed any.  So as soon as you get to an " i " value where there is no longer a child, you will encounter that error.

               

              One way to overcome it is to work the loop in reverse, going from the highest child index to the lowest, as in....

               

                      for(var i:int = ilosc-1, i >-1; i--)