2

我对为什么 get(Object obj) 不能与实例变量(实例字段)一起使用并抛出 IllegalArgumentException 感到困惑。

我有以下课程:

public class InfoSender {

String testInt = "hey";

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    InfoSender test = new InfoSender();
    test.asd();
    //        script g = new script();
    //        mobile h = new mobile();
}

private void asd() {
    new StatusGUI(this);
}

}

我有 StatusGUI 类(不包括 initComponents):

public class StatusGUI extends javax.swing.JFrame {

Class clazz;

/** Creates new form StatusGUI */
public StatusGUI(Object invokerObject) {
    initComponents();
    setVisible(true);
    setLocationRelativeTo(null);
    clazz = invokerObject.getClass();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            jTextArea1.append(fields[i].getName() + " | " + fields[i].getType() + " | " + fields[i].get(clazz));

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}                                        
}

按下按钮后,它将成功写入字段名称和类型。

当我想添加公共字段的值时,我很快了解到如果不将它们声明为静态(至少据我所知不是),这是不可能的。

尽管根据 Java 字段类文档,该字段可以作为实例字段:http: //download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field。 html

异常的堆栈跟踪

java.lang.IllegalArgumentException: Can not set java.lang.String field infosender.InfoSender.testInt to java.lang.Class
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18)
at java.lang.reflect.Field.get(Field.java:358)
at infosender.StatusGUI.jButton1ActionPerformed(StatusGUI.java:93)
at infosender.StatusGUI.access$000(StatusGUI.java:22)
at infosender.StatusGUI$1.actionPerformed(StatusGUI.java:54)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6289)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6054)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4652)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

感谢任何帮助:) 迈克。

4

2 回答 2

11

简而言之,问题在于您使用Field.get()错误。你是说:

fields[i].get(clazz)

...clazz您尝试从中获取字段值的 Object 的类在哪里。这就是为什么您只能看到静态字段的原因。你想要做的是这样的:

fields[i].get(invokerObject)

要获取每个实例属性的值,您需要传递要从中提取值的 Object 的实例,而不是Object 的class的实例。

请注意,您还可以通过在调用之前调用来获取privateprotected字段的值。fields[i].setAccessible(true)fields[i].get()

于 2011-07-30T12:16:07.857 回答
4

该方法Field#get(Object)采用实际对象来提取值,而不是Class. 所以应该是

fields[i].get(invokerObject)

所以invokerObject应该设置为一个字段,StatusGUI以便它可以传递给您的jButton1ActionPerformed方法。

于 2011-07-30T12:13:45.320 回答