我很疑惑爲什麼get(Object obj)不能與實例變量(實例字段)一起工作,並拋出IllegalArgumentException。get(Object obj)of field not working with instance variables
我有以下類:
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)
欣賞任何幫助:) 邁克。
非常具有描述性和清晰的答案,謝謝:) –