BitmapData not working

I have a Bullet class that I call every time my shoots a bullet, but I am getting this error: “TypeError: Error #1009: Cannot access a property or method of a null object reference”.

This is a small snippet of code of where I am having trouble with my class:

[AS]
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

public class Bullet {
	
	public var x:Number;
	public var y:Number;
	public var speed:uint = 5;
	
	public var texture:BitmapData;
	public var rec:Rectangle = new Rectangle(0, 0, texture.width, texture.height);
	public var point:Point = new Point(0, 0);
	
	public function Bullet(x1:Number, y1:Number, c:uint) {
		
		x = x1;
		y = y1;
		texture = new BitmapData(5, 5, false, c);
		
	} // end function

    }

[/AS]

The problem occurs when I try to pass the BitmapData fillColor value. If I were to create the BitmapData where I first declare the variable “texture” and give it a color, it will work like a charm. However, I want to set the fillColor dynamically because of who is shooting the bullet. ie, heroes bullets are green, enemies are red.

Why am I getting this error and how do I fix it?

you declared texture as a BitmapData object but gave it no value, so its default is null. Directly after that you try to set the rec based off of the width and height of texture - texture which is null. That doesn’t work so you get the error.

You need to either define texture first, or use some other width/height values

o yes, thank you Senocular… I knew it was something dumb and stupid of me (which is very often). This is my fix:

[AS]
public var texture:BitmapData;
public var rec:Rectangle = new Rectangle(0, 0, 0, 0);
public var point:Point = new Point(0, 0);

    public function Bullet(x1:Number, y1:Number, c:uint) {
        
        x = x1;
        y = y1;

        texture = new BitmapData(5, 5, false, c);

        rec.width = texture.width;
        rec.height = texture.height;
        
    } // end function

[/AS]

FYI the BitmapData class has a ‘rect’ property which returns it’s rectangle.