开发者

Using class.forname().newInstance() and "com.sun.jdi.InvocationException occurred invoking method" exception

I am using class.forname to create a new instance of class.The classname comes from a properties.

Lets say i have several classes in two packages.

com.package.Parser1
com.package.Parser2
com.package.Parser3

net.package.parser4
net.package.parser5
net.package.parser6

The following classes also exist in the above packages (This are not instantiated)

com.package.ParserLoader
com.package.ParserInterface
net.package.GenericParser

On initialisation, the above parser are put in a vector. The vector is t开发者_如何学Gohen accessed and each class is initialised using its class name as shown below

while (tokens.hasMoreTokens())

        parsers.addElement(

            Class.forName((String) tokens.nextToken()).newInstance());
    }catch(Exception e){
        e.printStackTrace();
    }

The code above is in the class ParserLoader which is in the same package as parsers 1,2,3.

Parsers 1, 2 and 3 all implement the ParserInterface. Parsers 4,5 and 6 all extends the abtract GenericParser. The GenericParser implements the ParserInterface.

When i run the above it generates an exception shown below

com.sun.jdi.InvocationException occurred invoking method

Any ideas why this is happening?

Edit

Another problem i am having is i cant see any stack trace. There is no stacktrace! I only see that error in eclipse when i debug the application and look at the content of the parsers vector. The vector should contain references to the parser objects. The parsers in com.package.* package are fine but it is not creating instances of any parser in the net.package.* package.


I think you'll get this exception if your constructors are messed up or something else is broken while creating the parsers. Check this out: http://download.oracle.com/javase/6/docs/jdk/api/jpda/jdi/com/sun/jdi/InvocationException.html, and try to get more info out of your exception.


com.package.ParserInterface sounds like a interface, and you are not able to create an instance from an interface.


If you are getting com.sun.jdi.InvocationException in the value column of the Variables tab of the debug pane, it is most likely that your Preferences-Java-Debug-Detail Formatters are set to display toString() for the values of variables.

If the object is not fully built yet, but toString() calls a method with not yet available data, an invocation exception is expected and helpful.

To see a nicer but a redundant text, check the In detail pane only option (or whatever is relevant to your version of Eclipse).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜