# BitmapData not working

**URL:** https://forum.kirupa.com/t/bitmapdata-not-working/262955
**Category:** flash
**Created:** [June 11, 2008, 12:38pm UTC](https://forum.kirupa.com/t/bitmapdata-not-working/262955 "2008-06-11T12:38:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gregmax](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@gregmax](https://forum.kirupa.com/u/gregmax)
#### Post date: [June 11, 2008, 12:38pm UTC](https://forum.kirupa.com/t/bitmapdata-not-working/262955/1 "2008-06-11T12:38:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [June 11, 2008, 1:01pm UTC](https://forum.kirupa.com/t/bitmapdata-not-working/262955/2 "2008-06-11T13:01:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![gregmax](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@gregmax](https://forum.kirupa.com/u/gregmax)
#### Post date: [June 11, 2008, 1:52pm UTC](https://forum.kirupa.com/t/bitmapdata-not-working/262955/3 "2008-06-11T13:52:04Z")

</div>

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]

---

<div class="post-metadata">

### Author: ![substance](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@substance](https://forum.kirupa.com/u/substance)
#### Post date: [June 11, 2008, 5:55pm UTC](https://forum.kirupa.com/t/bitmapdata-not-working/262955/4 "2008-06-11T17:55:36Z")

</div>

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