Skip navigation
Currently Being Moderated

Error #1009: Cannot access a property or method of a null object reference.

Apr 13, 2010 6:16 AM

can anybody help please?

 

in output i have:

+++ Welcome screen +++
+++ Levels screen +++
>>> array: 0,0,1,1
>>> random numbers: 3
>>> random numbers: 2
>>> random numbers: 1
>>> random numbers: 0
****** Icons creationg finished ******
Level: 1 started
TypeError: Error #1009: Cannot access a property or method of a null object reference.
     at MyMatching/stopwatch()[D:\#_Flash_Games\MatchingThings\MyMatching.as:70]
Icon 0 was clicked
Icon 0 was clicked
+ added 20 points because matching of 2 icons. Now player has: 20 points
--- deleted 2 icons ---
Icon 1 was clicked
Icon 1 was clicked
+ added 20 points because matching of 2 icons. Now player has: 40 points
--- deleted 2 icons ---
+++ Gameover Screen +++

 

code:

package 
{
     import adobe.utils.CustomActions;
     import flash.display.MovieClip;
     import flash.events.MouseEvent;
     import flash.text.TextField;
     import flash.events.*;
     import flash.utils.getTimer;
 
     public class MyMatching extends MovieClip
     {
          public var xIconsContainer;//all icons start to draw from this x
          public var yIconsContainer;//all icons start to draw from this y
          
          private var iconsVertical=2;//icons columns
          private var iconsHorizontal=2;//icons rows
          
          private var firstIcon;//player click to first icon
          private var secondIcon;//player click to second icon
          
          private var deletedIcons:int = 0;//if firstIcon==secondIcon we will delete 2 icons
          public var numberOfLevels:int = 2;//2 for 1 level; 4 for 1, 2 levels; 6 for 1, 2, 3 levels
          
          public var startTime:int = getTimer();
          public var timeString:String;
          
          public var playerScoreSum:int = 0;//sum of all players points
          
          public var iconsList:Icon;
          public var iconsArray:Array;
          
          public var iconWidth:int = 51;//width of icon
          public var iconHeight:int = 51;//height of icon
          
          public var levelNumber:int = 1;
          
          public var matchPoints:int = 20;//if player match 2 icons we add 20 points to his playerScoreSum
          public var missPoints:int = 1;//if miss we delete 1 points from playerScoreSum
          
          //constructor
          public function MyMatching()
          {
               welcomeScreen();
          }
          
          public function welcomeScreen():void
          {
               trace("+++ Welcome screen +++");
               gotoAndStop('WelcomeScreen');
               playBtn.addEventListener(MouseEvent.CLICK, goToLevels);
          }
          
          public function startingStopwatch():void
         {
               addEventListener(Event.ENTER_FRAME, stopwatch);
         }
          
         public function stopingStopwatch():void
         {
               removeEventListener(Event.ENTER_FRAME, stopwatch);
         }
          
        public function stopwatch(event:Event) 
        {
               var timePassed:int = getTimer()-startTime;
               var seconds:int = Math.floor(timePassed/1000);
               var minutes:int = Math.floor(seconds/60);
               seconds -= minutes*60;
               timeString = minutes + ":" + String(seconds + 100).substr(1, 2);
               stopwatchPreview.text = String(timeString);
        }
          
          public function goToLevels(event:MouseEvent)
          {
               xIconsContainer=stage.stageWidth/2 - (iconWidth*iconsHorizontal)/2;//all icons start to draw from this x
               yIconsContainer=stage.stageHeight/2 - (iconHeight*iconsVertical)/2;//all icons start to draw from this y
               startingStopwatch();
               
               trace("+++ Levels screen +++");
               iconsArray = new Array();//creating new object iconsArray     
               
               gotoAndStop('Levels');
               
               for (var i:int; i < iconsVertical*iconsHorizontal/2; i++)
               {
                    iconsArray.push(i);
                    iconsArray.push(i);
               }
               trace(">>> array: " + iconsArray);               
               
                    
               
               for (var x:int = 0; x < iconsVertical; x++)
               {
                    for (var y:int = 0; y < iconsHorizontal; y++)
                    {
                         iconsList = new Icon();
                         addChild(iconsList);
                         iconsList.buttonMode = true;//if we move cursor to icon we can see hand
                         iconsList.stop();//stoping our icons on Flash IDE, because on one layer we have all our icons
                         iconsList.x=x*iconWidth+xIconsContainer;
                         iconsList.y=y*iconHeight+yIconsContainer;
                         var myRandom:int=Math.floor(Math.random()*iconsArray.length);//creates a random number that will be related to an index of the array named "iconsArray"
                         //var showIcon;//cast as whatever type of element the array is holding
                         iconsList.showIcon=iconsArray[myRandom];//assigns the randomly selected element of iconsArray to the variable showIcon
                         iconsArray.splice(myRandom,1);//removes the randomly selected element (now showIcon) from the array (not the last one, the random one) //we use the splice command to remove number from the array so that it won’t be used again
                         //iconsList.gotoAndStop(iconsList.showIcon+2);//showIcon+2 would be the next frame in the timeline after the frame for the random item deleted, so that code is essentially moving in the timeline to the frame just after the frame for the item that was removed from the array
                         trace(">>> random numbers: " + myRandom);
                         iconsList.addEventListener(MouseEvent.CLICK, clickToIcon);
                         deletedIcons++;//when we draw icons we every time add +2 icons to our deletedIcons variable, in future we will deleted 2 icons from this variable if firstIcon==secondIcon
                    }
               }
               
               //added score to the sceen
               trace("****** Icons creationg finished ******");
               trace("Level: " + levelNumber + " started");
               //levelNumberPreview.text = String(levelNumber);               
          }          
          
          public function clickToIcon(event:MouseEvent)
          {
               var thisIcon:Icon = (event.currentTarget as Icon);//what icon player clicked...
               trace("Icon " + thisIcon.showIcon + " was clicked");//trace clicked icon to the output pannel
               if (firstIcon==null)
               {
                    firstIcon=thisIcon;
                    firstIcon.gotoAndStop(thisIcon.showIcon + 2);
               } 
               else if (firstIcon == thisIcon)
               {
                    firstIcon.gotoAndStop(1);
                    firstIcon=null;
               } 
               else if (secondIcon==null)
               {
                    secondIcon=thisIcon;
                    secondIcon.gotoAndStop(thisIcon.showIcon + 2);
                    
                    if (firstIcon.showIcon==secondIcon.showIcon)
                    {
                         playerScoreSum += matchPoints;// add matchPoints for all existing player points
                         scoreGamePreview.text = String(playerScoreSum);
                         trace("+ added " + matchPoints + " points because matching of 2 icons. " + "Now player has: " + playerScoreSum + " points");
                         
                         removeChild(firstIcon);
                         removeChild(secondIcon);
                         deletedIcons -= 2;
                         trace("--- deleted 2 icons ---");
                         if (deletedIcons == 0)
                         {
                              if (iconsVertical >= numberOfLevels)
                              {
                                   iconsVertical = 2;
                                   iconsHorizontal = 2;
                                   levelNumber = 1;
                                   MovieClip(root).playerScoreSum = playerScoreSum;
                                   stopingStopwatch();
                                   startTime = getTimer();
                                   gotoAndStop('GameoverScreen');
                                   trace("+++ Gameover Screen +++");
                              }
                              else
                              {
                                   iconsVertical += 2;
                                   iconsHorizontal += 2;
                                   levelNumber += 1;
                                   xIconsContainer+=2;
                                   yIconsContainer+=2;
                                   goToLevels(null);
                              }                              
                         }
                    }
                    else
                    {
                         playerScoreSum -= missPoints;// delite missPoints from all existing player points
                         scoreGamePreview.text = String(playerScoreSum);
                         trace("- deleted " + missPoints + " points because missing of 2 icons. " + "Now player has: " + playerScoreSum + " points");
                    }
               }
               else
               {
                    firstIcon.gotoAndStop(1);
                    secondIcon.gotoAndStop(1);
                    firstIcon = null;
                    secondIcon = null;
                    firstIcon=thisIcon;
                    firstIcon.gotoAndStop(thisIcon.showIcon + 2);
               }
          }
          
          
     }
}
 
Replies
  • Currently Being Moderated
    Apr 13, 2010 7:03 AM   in reply to Sergey Landar

    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 not in the display list
    - 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 different names assigned.
     
    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.
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 7:25 AM   in reply to Sergey Landar

    All is not okay if that error message is coming up.  If you can isolate the single line of code that the error message points to, I might be able to suggest something, but I won't be reading thru all of your code to try to figure it out.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 8:21 AM   in reply to Sergey Landar

    Put some traces ahead of that line of code...

     

    trace("stopwatch is MIA", stopwatchPreview);

    trace("timeString is MIA", timeString);

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 12:12 PM   in reply to Sergey Landar

    what is stopwatchPreview? is it textfield?  where do you declrare it? something like "private var stopwatchPreview:textField;"

    if it is a movieclip on timeline you can pass it to your class when create new MyMatching  (changing constructor to "public function MyMatching(_stopwatchPreview:TextField){....... )

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 9:31 PM   in reply to Sergey Landar

    try this :-

     

    if ( stopwatchPreview ! = null ) before  stopwatchPreview.text = String(timeString); at line # 70

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 14, 2010 9:11 PM   in reply to Sergey Landar

    Welcome

     

    Enjoy Flashing.....

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points