【发布时间】:2014-05-23 06:33:55
【问题描述】:
我想用反射调用 getter。我是这样累的:
for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors()){
Method m = propertyDescriptor.getReadMethod();
if(m != null)
System.out.println(m.invoke(this).toString());
}
我总是在调用该方法的行中得到 NullPointerException。
异常的堆栈跟踪:
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
我搜索了这个问题,但没有找到解决方案。
【问题讨论】:
-
this和实例是什么?m叫什么名字? -
你没有先创建实例...
-
我搜了一下,发现了一个了不起的similar question
-
这是我自己的班级,在本例中为
Block。我有很多要使用此代码的类。我在 Block 类中直接尝试过。如果它可以工作,我想把它放在超类中,所以我只需要编写这个方法一次。在m中有所有的getter 方法。我首先创建了实例。
标签: java reflection getter