简体   繁体   中英

Java - Force Instantiation on Class

I keep getting the error "InstantiationException" when I try to run this code below from my custom deserializer:

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);
}

Weird thing is that it worked at one point and now it doesn't.

How do I force the newInstance and then set the fields in that object later (which readObject does and is my own function)?

Here is some debug info:

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)

UPDATE 1

Ok, since the class Color needs aruguments, how I pass default stuff to a class if a class needs aruguments automatically? Or is it possible to inject a default constructor?

Thanks.

newInstance() requires a parameterless constructor, and Color has no such constructor.

You need to find an appropriate Constructor , and use that.

One of the reasons should be that you try to call static method and you don't need to call newInstance() .

For example:

....
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);
....

Other reason , you didn't pass arguments to constructor (verify if you have default)

for arguments use:

 MyClass c = MyClass .class.getConstructor(String.class).newInstance("some string"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM