• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

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()
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);
               }
          }
          
          
     }
}

TOPICS
ActionScript

Views

2.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Apr 13, 2010 Apr 13, 2010

try this :-

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

Votes

Translate

Translate
LEGEND ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

yes, i see line(added output info in first post)

but can not understand how to fix it.

looked to your list, but as for me all ok.

but still see error:(

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

stopwatchPreview.text = String(timeString);  << error message in output window show to this line.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

by the way

in program visual all working correct, and i dont see any errors.

but i see this error in output window

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

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

trace("stopwatch is MIA", stopwatchPreview);

trace("timeString is MIA", timeString);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2010 Apr 14, 2010

Copy link to clipboard

Copied

Ned

here result:

stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]
timeString is MIA 0:00
stopwatch is MIA [object TextField]

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

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){....... )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 13, 2010 Apr 13, 2010

Copy link to clipboard

Copied

try this :-

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2010 Apr 14, 2010

Copy link to clipboard

Copied

timer working

dont see error in output

used this:

if ( stopwatchPreview != null )

thanks Kartik

thanks everyone!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 14, 2010 Apr 14, 2010

Copy link to clipboard

Copied

LATEST

Welcome

Enjoy Flashing.....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines