tags:

views:

16

answers:

1

for some reason this flex 4 code gives me an error but I can't figure out why. In my WindowedApplication I have:

var prefs:Object = fs.readObject();
fs.close();
var rect:Rectangle = new Rectangle();
rect = prefs.bounds as Rectangle;
this.bounds = rect;  // error here

Error message: ArgumentError: Error #2007: Parameter rect must be non-null.

I originally also tried it without the rect object and just did:

this.bounds = prefs.bounds as Rectangle;

which gives me the following error:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@1dbbe1f1 to flash.geom.Rectangle.

That seems like a bogus error since I can assign pref.bounds to rect without an error. I don't know why this isn't working. It works under flex 3 compatibility mode but that also breaks a lot of my spark components so I can't use it.

A: 
rect = prefs.bounds as Rectangle;

rect ends up as null because prefs.bounds is not an instance of Rectangle class. Casting with as keywords returns null if it fails.

Just give a trace(prefs.bounds); after the call to readObject to see what it contains.


Update:

The fact that the value in prefs.bounds have x, y, width and height properties, doesn't make it a Rectangle object - it is just an object with those four properties. The easy solution is to create a Rectangle object from those values and use that:

var prefs:Object = fs.readObject();
var rect:Rectangle = new Rectangle(prefs.bounds.x, prefs.bounds.y, prefs.width, prefs.height);
this.bounds = rect;

If you call registerClassAlias before writing the rectangle object to the file stream, the readObject would return an object of appropriate type instead of a generic one.

registerClassAlias("flash.geom.Rectangle", Rectangle);
fs.writeObject(this.bounds);

//later...
//Casting with () will throw an error if the read object is not a Rectangle
var rect:Rectangle = Rectangle(fs.readObject());
trace(rect);
Amarghosh
so you're saying that:this.bounds = rect; equates to:this.bounds = null;And the windowedApp doesn't accept that? When I use the debugger, prefs.bound has all of the fields you'd expect, width, height, x, y, etc. So if it's not a Rectangle, I don't know what it would be.The prefs object by the way is actually generated originally from this.bounds and saved to a file. That's why when I read it back with this code, I am surprised it is choking like this.
solerous
@solerous see my update.
Amarghosh
Hey thanks for the great responses. Unfortunately, the code doesn't work.var rect:Rectangle = Rectangle(fs.readObject());trace(rect);gives me the error:TypeError: Error #1034: Type Coercion failed: cannot convert Object@1db7bdf9 to flash.geom.Rectangle.Even though I use:registerClassAlias("flash.geom.Rectangle", Rectangle);fs.writeObject(this.bounds);to save it initially.I just don't understand why AIR can't do this kind of 1:1 conversion. This code is actually taken from an app in Flex 3 and it worked perfectly there. I just don't know what has changed in Flex 4.
solerous
Hey, actually, I tried it again, and it works!What I was missing was this line in the init() method:flash.net.registerClassAlias("flash.geom.Rectangle", flash.geom.Rectangle);I think putting it there meant that it worked for both WRITE and READ operations.Thanks for the help!
solerous