What Causes Flash Error #2012 (Can't instantiate class)?
Asked Answered
G

3

13

I am new to ActionScript 3 and have run into a problem:

Using Flex Builder 3, I have a created a project with a few simple classes. If code in class A instantiates an object of class B (class B is in its own source file) then the code compiles fine, but I get the following run time error:

ArgumentError: Error #2012: B class cannot be instantiated.

Can someone explain what I'm doing wrong?

Update: Please see my own answer below (I could not vote it to the top since I'm not yet registered).

Groves answered 16/9, 2008 at 0:28 Comment(0)
G
21

I finally realized what was wrong: Class B was subclassing from DisplayObject which I now see is an abstract class. Class B did not implement the abstract members, thus the error. I'll probably change class B to subclass from Sprite instead.

This seems like a problem that should have been caught by the compiler. Does the fact that it wasn't mean implementation of abstract members can wait until run time? Even if so, it would be nice to at least get a compiler warning.

Thanks for everyone's answers, hopefully they will help others who run into error 2012.

Groves answered 16/9, 2008 at 2:58 Comment(1)
It can't be caught by the compiler since there is no keyword abstract that the compiler can pick it up on. It is enforced by throwing, hence the error.Lichenology
R
2

This usually means that the class information was not included in the SWF.

Make sure that you are importing the class, and that there is a reference to it somewhere (so the compiler will included it in the SWF).

btw, here are the runtime error codes:

http://livedocs.adobe.com/flex/201/langref/runtimeErrors.html

(not much useful info though)

mike chambers

[email protected]

Recount answered 16/9, 2008 at 0:44 Comment(0)
B
0

It's worth noting that if you're including classes that someone else built, and they used Flash CS3 and you're using Flex, or vice versa, that the core libraries of each are different and some things are not included in both. Check out the two reference docs to be sure:

CS3: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/

Flex: http://livedocs.adobe.com/flex/2/langref/

Byers answered 16/9, 2008 at 1:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.