Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am relatively new to object oriented programming in ActionScript 3.0..I designed some cloud and I am trying to control the movement of the cloud via a class. The code I have used is from a tutorial I followed online. I created two Scenes in flash (start scene and Main scene) and I have a class called Cloud.as..The cloud movements work fine in the start scene but once I hit the start button to go in the next scene, I am having an error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Cloud/wrapCloud()[Project\Jack_Crawford_System\Cloud.as:46]
at Cloud/onEnter()[Project\Jack_Crawford_System\Cloud.as:37]

Can anyone please tell me what should I do? There is no code in the Main scene

Here is the code for the Cloud.as:

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

    public class Cloud extends MovieClip
    {
        private var _cloudpixel:Number;
        private var _halfCloudWidth:Number;

        public function Cloud()
        {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(event:Event):void
        {
            _cloudpixel = -1;
            _halfCloudWidth = this.width/2;

            addEventListener(Event.ENTER_FRAME, onEnter);
            addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
        private function onRemovedFromStage(event:Event): void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        }
        private function onEnter(event:Event):void
        {
            this.x +=_cloudpixel;
            wrapCloud();
        }
        private function wrapCloud():void
        {
            if (this.x + _halfCloudWidth<0)
            {
                this.x = stage.stageWidth + _halfCloudWidth;
            }
        }
    }
}


Thank you!
Posted

1 solution

Hi,

The problem lies in wrapCloud() method, line 46 : either this is null, or stage is null.
 
Share this answer
 
Comments
Member 9586624 17-Jan-13 8:02am    
Thank you for your answer. Do you have any idea on how to solve it?
phil.o 17-Jan-13 9:39am    
Yes, sure : first you have to find which variable is uninitialized ; second, you need to initialize it before attempting to access its member properties/methods.
But I don't have any clue about how to find which variable is uninitialized ; it depends on the IDE you are using, and whether or not you can debug your code. Basically, the idea is to launch your application in debug mode, and watch for the value of your variables to find which one is causing the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900