当我尝试从自定义反序列化程序运行以下代码时,我不断收到错误“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类需要使用aruguments,那么如果一个类自动需要语句,我如何将默认的东西传递给一个类?或者是否可以注入默认构造函数?
感谢。
答案 0 :(得分:9)
newInstance()
需要无参数构造函数,Color
没有这样的构造函数。
您需要找到合适的Constructor
并使用它。
答案 1 :(得分: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");