A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Why is this giving me an error?!

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    60

    Unhappy Why is this giving me an error?!

    Code:
    package
    {
    	import flash.display.Sprite;
    	import flash.events.*;
    	public class MagnetGolf extends Sprite
    	{
    		public var puck:Puck;
    		public var puckVX:Number;
    		public var puckVY:Number;
    		public var minusCharges:Array;
    		
    		public function MagnetGolf()
    		{
    			puck = new Puck();
    			addChild(puck);
    			puck.x = 20;
    			puck.y = 240;
    			
    			puckVX = 1;
    			puckVY = 0;
    			
    			plusCharges = new Array();
    			for (var i:int = 0; i < 5; ++i)
    			{
    				var c:MinusCharge = new MinusCharge();
    				addChild(c);
    				c.x = Math.random() * 550;
    				c.y = Math.random() * 400;
    				minusCharges.push(c);
    			}
    			
    			addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    		}
    		
    		function update(e:*=null):void
    		{
    			puck.x += puckVX;
    			puck.y += puckVY;
    		}
    	}
    }
    The error is one of the output ones, which give no info on why I get the error.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at MagnetGolf()
    Something that might help: If I take away the line minusCharges.push(c); it doesn't give me that error. So I assume c doesn't exist yet?? But i added it a couple of lines up with addChild! Help please!

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    You are not creating your array 'minusCharges'.

    Add minusCharges = new Array(); above the for loop.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    60
    Whoops. Thank you for the assistance. It wasn't c, then.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center