Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-23-2012, 05:04 PM   #1
Lay
Registered User
 
Join Date: Mar 2012
Posts: 9
Default Cannot access a property or method of a null object reference

Hi there,

Part of my Actionscript 3.0 course has asked me to research why the following happens. Would anyone be able to help me out? Im very new to actionscript so be gentle!

My Kernel class is the document class of the .fla file, inside the Kernel constructor I create an instance of a new class myClass1 and add it to the stage. Inside the constructor of myClass1 I try to trace(stage.stageWidth) and when I run the program I get the error "Cannot access a property or method of a null object reference", but there is no compile error.

Why does this happen?
Lay is offline   Reply With Quote
Old 03-23-2012, 05:27 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Short answer: Because each lines of code must be executed before moving on to the next one.
Explanation: In your example, you create an instance of myClass1 and on the next line add it to the Display list but before it is added it does run the code that is in its constructor and in that code there is a reference to stage which at this point is null because that instance is not yet on the Display List. So that's how it goes:
ActionScript Code:
var classone:Class1 = new Class1();//all code in constructor runs addChild(classone);//now classone has a valid stage
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 03-23-2012, 05:41 PM   #3
Lay
Registered User
 
Join Date: Mar 2012
Posts: 9
Default

Thank you very much for the reply, I completely understand.

Thanks.
Lay 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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:54 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.