1 Replies - 988 Views - Last Post: 04 March 2018 - 05:06 AM

#1 BandaraDLM   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-March 18

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

Posted 04 March 2018 - 03:38 AM

please tell me how to solve this error --> TypeError: Error #1009: Cannot access a property or method of a null object reference.

following is my code i created it by referring a tutorial

package
{
import flash.display.MovieClip;
import flash.events.Event;

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

/**
* ...
* @author Lahiru
*/
public class firstGame extends MovieClip
{


private var leftKeyIsDown:Boolean;
private var rightKeyIsDown:Boolean;
public var Gun1:MovieClip;

public function firstGame()
{

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
stage.addEventListener(Event.ENTER_FRAME, gameLoop)


}

private function gameLoop(e:Event):void
{
//if left key is currently down
if (leftKeyIsDown == true){
Gun1.x -= 5;
}

//if right key is currently down
if (rightKeyIsDown){

Gun1.x += 5;
}
}

private function KeyUp(e:KeyboardEvent):void
{
trace(e.keyCode)

//if key left key is released
if (e.keyCode == 37)
{
leftKeyIsDown = false;
}

//if right key is released
if (e.keyCode == 39){

rightKeyIsDown = false;
}
}

private function KeyDown(e:KeyboardEvent):void
{
//if left key is pressed
if (e.keyCode == 37){
leftKeyIsDown = true;
}

//if right key is pressed
if (e.keyCode == 39){
rightKeyIsDown = true;
}
}

}

}

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at firstGame/gameLoop(

Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: Error #1009: Cannot access a property or method of a null

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 977
  • View blog
  • Posts: 3,855
  • Joined: 13-June 14

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

Posted 04 March 2018 - 05:06 AM

I don't know ActionScript, but a couple of things

1. Please remember to post code within "[CODE]" tags as you're asked in the box where you type your posts.
2. Post error messages in their entirety. I'd expect to see a line number at least in the error message you've posted.

The error seems to refer to the gameLoop function, so you'll need to work out what's null in there and work backwards to see why that reference is null. Again, the error message should be pointing you to the line on which you tried to dereference the null reference, so it should be pretty easy to spot. As an educated guess, I don't see anywhere you set values for your member variables leftKeyIsDown, rightKeyIsDown and Gun1; I'd expect these to be initialised in the constructor. So, I suspect that one of those is what's causing the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1