Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

BitmapData and Error #1009

New Here ,
Nov 15, 2010 Nov 15, 2010

I have a little function:

public function makeBMD(dataObject,displayObject):void {

   dataObject = new BitmapData(displayObject.width,displayObject.height, true, 0x00000000);

   dataObject.draw(displayObject);

   var bmp:Bitmap = new Bitmap(dataObject);

   addChild(bmp);

}

Looks simple enough. I pass it a BitmapData variable (silBMD) and a MovieClip (theSilContainer). I want it to create a new BitmapData object, which I think it does since it also creates and adds the bmp to stage using the BitmapData object.

But, I have this other function:

public function testSolution():void {

   makeBMD(playBMD,thePlayContainer);

   var diffBmpData:BitmapData = silBMD.compare(playBMD) as BitmapData;

   trace ("0x" + diffBmpData.getPixel(100,100).toString(16)); // 0x332200

}

This creates a new object (just like it did to the first one) so that I can compare the two objects. What is happening when it gets called, however, is that I receive this error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.

Testing inside the makeBMD function for height and width will trace out properly as long as I trace out the references (dataObject) but not the actual variables passed in (silBMD or playBMD). But once the object is referenced anywhere else, it it fails.
Any ideas what I'm doing wrong here? I saw some other threads where errors are thrown because a loader event hasn't completed, but I'm not using a loader here.

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2010 Nov 15, 2010

the only thing passed to either function is dataObject.  and, from the code you showed, it doesn't appear playBMD or silBMD is non-null.

anyway, click file/publish settings/flash and tick "permit debugging".  the line number with your (first) null object will be in the error message.  fix that and work on the next (if any) error message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 15, 2010 Nov 15, 2010

Hey Kglad, thanks for the quick response.

Both silBMD and playBMD are defined like so:

package {

   import flash.display.*;

   import flash.geom.*;

   import flash.events.MouseEvent;

   import flash.text.*;

   import fl.controls.*;

   public class Test extends MovieClip {

    var container = this;

.

.

.

     // the solution bmds

     var silBMD:BitmapData;

     var playBMD:BitmapData;

The makeBMD function is called ONLY after the theSilContainer and thePlayContainer MovieClips have been added to the display list.

The debug doesn't tell me anything new. Even instantiating the silBMD and playBMD as empty bitmapdata returns the null reference error.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2010 Nov 15, 2010

both of those are null.  they are typed but they are null and that's what your error message is telling you.

you need to instantiate both of those before you try and use them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 15, 2010 Nov 15, 2010

I still don't understand what I'm doing here.

If I do this in the function

var silBMD:BitmapData = new BitmapData(theSilContainer.width,theSilContainer.height, true, 0x00000000);

silBMD.draw(theSilContainer);

trace(silBMD.width, silBMD.height);

var playBMD:BitmapData = new BitmapData(thePlayContainer.width,thePlayContainer.height, true, 0x00000000);

playBMD.draw(thePlayContainer);

trace(playBMD.width, playBMD.height);

var diffBmpData:BitmapData = silBMD.compare(playBMD) as BitmapData;

trace ("0x" + diffBmpData.getPixel(0,0).toString(16))

Everything traces out EXCEPT for the last trace statement, which throws the error. Also, if I set up silBMD and playBMD as BitmapData variables outside of the function, and pass them in as parameters (along with the MovieClip they're receiving the data from), it doesn't work.

And the only difference I can see between my code and the example adobe code is that mine uses the draw method to draw the movieClip into the bitmapdata object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2010 Nov 15, 2010

those two bitmaps must have the same width and height and everything must be in the same scope.  ie, those trace() outputs should be identical and those bitmapdata instances must be in the same timeline/function.  are they?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 15, 2010 Nov 15, 2010

They are not the same height and width. They are also not in the same function. But I can fix that...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2010 Nov 15, 2010

when you prefix a variable with var inside a function body, you're making that variable local to the function.  ie, it's not defined after that function executes.

in addition, you should read about the compare method so you can see the issue when the two bitmapdata objects have different size.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 15, 2010 Nov 15, 2010

Ah, I was aware of using var in a function. I did define the silBMD and playBMD in the root of the package before, and then create the data using the makeBMD function which is why I posted here at the beginning (see my 1st and 2nd post--the 2nd indicates the vars are set up in the package).

If you're saying that's the only reason why it's throwing an error, then I'm back to square one.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2010 Nov 15, 2010

in addition, you should read about the compare method so you can see the  issue when the two bitmapdata objects have different size.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 15, 2010 Nov 15, 2010
LATEST

Okay, I got this to work, but I still don't know why one approach works and he other does not.

   public function makeBMD(dataObject,displayObject):void {

   dataObject = new BitmapData(displayObject.width, displayObject.height, true, 0x00000000);

   dataObject.draw(displayObject);

   }

   public function makeBMDs():void {

   silBMD = new BitmapData(theSilContainer.width, theSilContainer.height, true, 0x00000000);

   silBMD.draw(theSilContainer);

  

   playBMD = new BitmapData(thePlayContainer.width, thePlayContainer.height, true, 0x00000000);

   playBMD.draw(thePlayContainer);

   }

   public function testSolution():void {

   makeBMDs();

  

   if (silBMD.height==playBMD.height && silBMD.width==playBMD.width){

           var diffBmpData:BitmapData = silBMD.compare(playBMD) as BitmapData;

           trace ("0x" + diffBmpData.getPixel(0,0).toString(16));

   }

   }

Passing silBMD and theSilContainer as paramaters to the makeBMD function always results in the error null.

Setting those same vars explicitly in he function makeBMDs works.

silBMD and playBMD are defined in the main package. Doesn't that make them globally available?

Anyway, thanks for all your attention to this thread.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines