1

当我尝试从我的自定义反序列化器运行以下代码时,我不断收到错误“InstantiationException”:

String attr = nel.getAttribute("class");
//happens when attr = "java.awt.Color"
if (attr != null && !attr.isEmpty()){
    Object no = Class.forName(attr).newInstance(); <--- dies here
    readObject(nel, no, true);
    field.set(obj,  no);
}

奇怪的是,它曾一度起作用,而现在却不起作用。

如何强制 newInstance 然后稍后在该对象中设置字段(哪个 readObject 是我自己的函数)?

这是一些调试信息:

java.lang.InstantiationException: java.awt.Color
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at wms.lsw.tool.CustomXMLDecoder.readObject(CustomXMLDecoder.java:240)
at wms.lsw.tool.CustomXMLDecoder.decode(CustomXMLDecoder.java:92)
at wms.lsw.tool.Utils.openProject(Utils.java:716)
at wms.lsw.tool.SecondaryMenuBar$2.actionPerformed(SecondaryMenuBar.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

更新 1

好的,由于类 Color 需要参数,如果一个类自动需要参数,我如何将默认的东西传递给一个类?或者是否可以注入默认构造函数?

谢谢。

4

2 回答 2

9

newInstance()需要一个无参数的构造函数,并且Color没有这样的构造函数。

你需要找到一个合适的Constructor,并使用它。

于 2012-12-03T20:09:24.847 回答
2

原因之一应该是您尝试调用静态方法并且不需要调用newInstance().

例如:

....
Class<?> c = Class.forName(attr);
Class[] argTypes = new Class[] { String[].class };
Method main = c.getDeclaredMethod("main", argTypes);
String[] mainArgs = Arrays.copyOfRange(args, 1, args.length);

main.invoke(null, (Object)mainArgs);
....

其他原因,您没有将参数传递给构造函数(验证您是否有默认值)

对于参数使用:

 MyClass c = MyClass .class.getConstructor(String.class).newInstance("some string"); 
于 2012-12-03T20:09:11.350 回答