使用 class.forname().newInstance() 和“调用方法时发生 com.sun.jdi.InitationException”例外
我正在使用 class.forname 创建类的新实例。类名来自属性。
可以说我在两个包中有几个类。
com.package.Parser1
com.package.Parser2
com.package.Parser3
net.package.parser4
net.package.parser5
net.package.parser6
以下类也存在于上述包中(未实例化)
com.package.ParserLoader
com.package.ParserInterface
net.package.GenericParser
在初始化时,上述解析器被放入向量中。然后访问该向量,并使用其类名初始化每个类,如下所示。
while (tokens.hasMoreTokens())
parsers.addElement(
Class.forName((String) tokens.nextToken()).newInstance());
}catch(Exception e){
e.printStackTrace();
}
上面的代码位于类 ParserLoader 中,该类与解析器 1、2、3 位于同一包中。
解析器 1、2 和 3 都实现了 ParserInterface。 解析器 4,5 和 6 都扩展了抽象 GenericParser。 GenericParser 实现 ParserInterface。
当我运行上面的代码时,它会生成如下所示的异常,
com.sun.jdi.InvocationException occurred invoking method
您知道为什么会发生这种情况吗?
编辑
我遇到的另一个问题是我看不到任何堆栈跟踪。没有堆栈跟踪!当我调试应用程序并查看解析器向量的内容时,我只在 Eclipse 中看到该错误。该向量应包含对解析器对象的引用。 com.package.* 包中的解析器很好,但它不会在 net.package.* 包中创建任何解析器的实例。
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 then 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为如果您的构造函数在创建解析器时被搞乱或其他东西被破坏,您将会得到这个异常。看看这个: http ://download.oracle.com/javase/6/docs/jdk/api/jpda/jdi/com/sun/jdi/InitationException.html,并尝试从异常中获取更多信息。
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
听起来像一个接口,并且您无法从接口创建实例。com.package.ParserInterface
sounds like a interface, and you are not able to create an instance from an interface.如果您在调试窗格的
Variables
选项卡的value
列中收到com.sun.jdi.InitationException
,则很可能是您的Preferences-Java-Debug-Detail
格式化程序设置为显示变量值的toString()
。如果对象尚未完全构建,但
toString()
调用尚不可用数据的方法,则预期会出现调用异常,并且会有所帮助。要查看更好但多余的文本,请选中
仅在详细信息窗格
选项(或与您的 Eclipse 版本相关的任何选项)。If you are getting
com.sun.jdi.InvocationException
in thevalue
column of theVariables
tab of the debug pane, it is most likely that yourPreferences-Java-Debug-Detail
Formatters are set to displaytoString()
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).