Take the 2-minute tour ×

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();
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.

share|improve this question
add comment

1 Answer

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.


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);

//Casting with () will throw an error if the read object is not a Rectangle
var rect:Rectangle = Rectangle(fs.readObject());
share|improve this answer
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 Jul 9 '10 at 18:28
@solerous see my update. –  Amarghosh Jul 10 '10 at 4:57
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 Aug 2 '10 at 20:20
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 Aug 2 '10 at 21:25
add comment

Your Answer


