dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

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

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

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

    Hey all. I checked Google and saw tons of results. Read some of the answers and tried to apply the solutions they used. Either it doesn't work or I'm doing something wrong.

    I kinda understand the error, but I don't know how to correct it.

    This is what I have in one frame. I know, I know...I'll use classes next time. Promise.

    The error only appeared when I implemented the following bit of code:

    Code:
    if(mcBall.hitTestObject(man))
    	{
    		gotoAndPlay("gameOver");
    	}
    Code:
    stop();
    
    // VARIABLES
    
    var ballXSpeed:Number = -8;
    var ballYSpeed:Number = -8;
    
    var starXSpeed:Number = 8;
    var starYSpeed:Number = 8;
    
    var leftKeyDown:Boolean = false;
    var upKeyDown:Boolean = false;
    var rightKeyDown:Boolean = false;
    var downKeyDown:Boolean = false;
    
    var manSpeed:Number = 7;
    
    // MOVE RED CIRCLE
    
    mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
    
    function moveBall(event:Event):void
    {	
    	mcBall.x += ballXSpeed;
    	mcBall.y += ballYSpeed;
    	
    	if(mcBall.x >= stage.stageWidth-mcBall.width)
    	{
    		ballXSpeed *= -1;
    	}
    	
    	if(mcBall.x < 0)
    	{
    		ballXSpeed *= -1;
    	}
    	
    	if(mcBall.y >= stage.stageHeight-mcBall.height)
    	{
    		ballYSpeed *= -1;
    	}
    	
    	if(mcBall.y <0)
    	{
    		ballYSpeed *= -1;
    	}	
    }
    
    
    // MOVE STAR
    
    mcStar.addEventListener(Event.ENTER_FRAME, moveStar);
    
    function moveStar(event:Event):void
    {	
    	mcStar.x += starXSpeed;
    	mcStar.y += starYSpeed;
    	
    	if(mcStar.x >= stage.stageWidth-mcStar.width)
    	{
    		starXSpeed *= -1;
    	}
    	
    	if(mcStar.x < 0)
    	{
    		starXSpeed *= -1;
    	}
    	
    	if(mcStar.y >= stage.stageHeight-mcStar.height)
    	{
    		starYSpeed *= -1;
    	}
    	
    	if(mcStar.y <0)
    	{
    		starYSpeed *= -1;
    	}
    	
    }
    
    man.addEventListener(Event.ENTER_FRAME, moveMan)
    
    // MOVE PLAYER
    
    function moveMan(event:Event):void
    {
    	Mouse.hide();
    	man.x = mouseX;
    	man.y = mouseY;
    }
    
    
    // COLLISION CIRLE AND MAN
    
    // The culprit for messing up
    
    stage.addEventListener(Event.ENTER_FRAME, circleHits);
    
    function circleHits(event:Event):void
    {
    	if(mcBall.hitTestObject(man))
    	{
    		gotoAndPlay("gameOver");
    	}
    }
    When the collision occurs, it goes to the game over screen but in flash it generates an infinite loop until I close the application.

    Here's the bit that loops.

    Code:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at game_fla::MainTimeline/moveBall()
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at game_fla::MainTimeline/moveStar()
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at game_fla::MainTimeline/moveMan()
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at game_fla::MainTimeline/circleHits()
    Thanks.

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    This would be easier looked at with a source file. Could you attach one?

    I will say this though, I do not see the mcBall variable declared anywhere.

  3. #3
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    It looks like mcBall does not exist. Did you forget to name it on your stage? Or is it on the stage at the time this script runs?

  4. #4
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks for the help guys.

    Also, in the gameOver screen I can't get the mouse to show (it's hidden during the game). I tried Mouse.show(); but it didn't work.
    Last edited by Hurdarr; 05-28-2009 at 09:23 AM.

  5. #5
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    just a hunch, remove eventlisteners before going to a new frame. what could happen is that the functions that are running from the enterframe are still running when you goto game over in which case things IN those functions may not exist anymore such as mcBall or ballXSpeed

    so for example:

    PHP Code:
    function circleHits(event:Event):void
    {
        if(
    mcBall.hitTestObject(man))
        {

            
    man.removeEventListener(Event.ENTER_FRAMEmoveMan)
            
    stage.removeEventListener(Event.ENTER_FRAMEcircleHits);
            
    mcStar.removeEventListener(Event.ENTER_FRAMEmoveStar);
            
    mcBall.removeEventListener(Event.ENTER_FRAMEmoveBall);

            
    gotoAndPlay("gameOver");
        }


  6. #6
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Yap. that did the trick. No more error messages. Thanks deadlock32.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center