0

Now Im saving to text format, and I got a error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at SaveImage/onClick()[/Users/VVT/Documents/Adobe Flash Builder 4.6/SuperDraw/src/SaveImage.as:40] I wanna change my code so I can save to PNG format?

public class SaveImage extends Sprite

{
    private var btnSave:buttonSave;
    //private var ba:ByteArray;
    private var file:FileReference;

    public function SaveImage()
    {
        // Skapar min knapp.
        var btnSave:buttonSave = new buttonSave();
        addChild(btnSave);
        btnSave.x = 400;
        btnSave.y = 440;

        btnSave.addEventListener(MouseEvent.CLICK, onClick);

        var file:FileReference = new FileReference();           
    }

    private function onClick(evt:MouseEvent):void 
    {
        //var ba:ByteArray = file.encode(bitmapData);
        //file.save(file);
        file.save("some text. \nsome more text", "actionsnippet.txt");
    }
}
PhatToni
  • 105
  • 1
  • 2
  • 12

1 Answers1

0

You have a property named file, yet you are creating and initializing a local variable with the same name in this line of your constructor:

var file:FileReference = new FileReference(); 

Don't worry, those mistakes happen. Remove the var and type to get rid of that null reference error.

file = new FileReference();

To save the image as png, the as3corelib library, which is mentioned in this answer of the question linked in this comment, looks quite promising. Import the library and let it encode your bitmapdata:

file.save(PNGEncoder.encode(bitmapData));
Community
  • 1
  • 1
kapex
  • 28,903
  • 6
  • 107
  • 121
  • How to install as3corelib, I been testing and I can not understand how I should install the as3corelib. Can somebody explain/show me how to do that? – PhatToni Jan 08 '12 at 09:55
  • @VVTinho I guess you have to add it the `as3corelib.swc` to the library paths in the ide. I never used Flashbuilder so I don't know how it's done exactly. A quick and dirty way would be simply to copy the `com` directory (found in `src` in the zip file) into your project's root directory. – kapex Jan 08 '12 at 12:03
  • `BitmapData.encode()` can encode PNG too, and doesn't require any external libraries. You can also decode PNG using `Loader`. JPEG is supported too. – osvein Oct 24 '17 at 19:29