Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-24-2012, 05:04 AM   #1
KingsGambit
Registered User
 
Join Date: Jun 2012
Posts: 10
Unhappy TypeError 1009 won't go away

I know that this is a common error that has been addressed but I've read all the threads and Adobe's library explanation of the error--
but I still cannot figure out what's wrong with my code (it worked earlier).
EDIT: So I think my debugger is saying my problem starts in the addEventListener line:
Code:
//this is the document class (MainCharacter.as), inside the Main Character class
		public function MainCharacter()
		{
			init();
		}
		public function init():void
		{
		        stage.addEventListener(KeyboardEvent.KEY_DOWN,ifKeyD);
			stage.addEventListener(KeyboardEvent.KEY_UP,ifKeyU);
			addEventListener(Event.ENTER_FRAME,ifEnterFrame);
		}
Output message:
Code:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at MainCharacter/init()
	at MainCharacter()

Last edited by KingsGambit; 06-24-2012 at 05:59 AM.
KingsGambit is offline   Reply With Quote
Old 06-24-2012, 07:27 AM   #2
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,408
Default

If you are adding this to another class, the reference to the stage in the init method is probably causing the error.
The stage is not a valid reference until the class instance is actually on the displaylist.

When you do this:
ActionScript Code:
var mainChar:MainCharacter = new MainCharacter();
The constructor is immediately called and a reference to the stage is made in the init() method, resulting in the error.

The stage is not available until you do this:
ActionScript Code:
addChild(mainChar);

The solution is listen for Event.ADDED_TO_STAGE in the MainCharacter class. This will trigger the Event handler when you call addChild(mainChar);
Make the reference to the stage in the Event handler.
[afz]snickelfitz is offline   Reply With Quote
Old 06-24-2012, 07:49 AM   #3
KingsGambit
Registered User
 
Join Date: Jun 2012
Posts: 10
Default

(edit) ah sorry, it's a doc class for the fla. I've read in other threads but none of them have the same problem as mine.

(edit2) It's actually because I had this object on stage with a code written for it (a health bar); I think the problem was that I didn't have an instance name for the health bar movieclip.

Thanks for the help though. I'm just following a beginner tutorial right now; I was curious how to call other classes with my document class.

Last edited by KingsGambit; 06-24-2012 at 09:55 AM.
KingsGambit is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:18 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.