1

I have a class called ChestScene that represents a scene/MovieClip in a .fla file I'm working on. I've had tons of issues that seem to be rooted in a fundamental misunderstanding of how to properly use objects that have code attached to them. It is my understanding that adding the object to the stage automatically instantiates it, so right now all I'm trying to do is instantiate and add to stage ChestScene() in the constructor of my Main method. I thought it would be as simple as this:

    public class Main extends MovieClip {

    var chest:Chest = new Chest();

    public function Main() {
        stage.addChild(chest);
    }

But I get this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Chest()/startScene()
    at Chest()
    at Main()

So my first question is why is Chest null? The object exists in my fla. If I add it to the stage by dragging from the library, the class works as intended. addChild seems to work on other objects that I am using the same way so I don't get why I can't use it on this object.

How to use the Main method to instantiate an object and access/change properties of said object relative to the stage? And how can I do the same thing for the objects nested inside of my initial object?

***** Edit after answer

Thanks for your reply and for teaching me how to read the debug message. startScreen() was indeed the culprit, and the issue there was this:

stage.addEventListener(Event.ENTER_FRAME, gameLoop)

Removing stage and adding the listener to the object fixed that error, so kudos! But I'm still confused; why does trying to add the listener to the stage cause a null error? I don't understand why the stage would be null at this point. Also, removing "stag." from the addEventListener caused the size of the frame that holds the scene to be way smaller, which cropped a lot of my image. Why is an event listener affecting my stageWidth and height? This is why I also asked questions about how to correctly position things relative to the stage width and height, because I know it has something to do with the errors.

  • "*I realize this is a large question*" too large in fact. Let's deal with the #1009 in this question. Ask the question related to scaling in a separate question. – null May 14 '16 at 12:57
  • You are misreading the error. It doesn't say chest is null, it says something inside the method startScene is null which I'm guessing is because you reference 'stage' which is null at that point. – BotMaster May 14 '16 at 13:11
  • As I said, please create a separate new question regarding your issues with scaling and sizing. Keep questions small and specific to a single issue. The issue of this question was why you received the error. Don't shy away from asking more questions. It is encouraged on this site. This site is not about having all your issues resolved in one huge mess. – null May 14 '16 at 21:55

1 Answers1

0

why is Chest null?

Chest is not null. It's the constructor of the class and cannot be null.

You should pay attention to the Stack trace attached to the error (the lines with "at …" below the message). It tells you where the error occurred. Reading it from the bottom up gives you the order of method calls performed.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Chest()/startScene()
    at Chest()
    at Main()

Main() called Chest(), Chest() called startScene() and BOOM! this is where the error occurred.

To debug this error look at startScene() . Something in there is null.

Looking at your current code:

stage.addChild(chest);

I guess that you also try to add something to stage in Chest(). But stage is not available ( null) in Chest(). In general, it's bad practice to add things to stage as can be read in the documentation of addChild()

Simply add to the object and not stage :

addChild(chest);
null
  • 5,207
  • 1
  • 19
  • 35
  • @pocketrockets please do not add code to comments, instead edit your question to include the additional information. – null May 14 '16 at 21:16