我正在修改程序,它有Class[] paramTypes
来存储输入类型。那我的问题是如何为数组的每个单元格赋值?
我可以执行此操作paramTypes = new Class[]{int.class,double.class,String.class}
为paramTypes分配,但是当我尝试为paramTypes[i] = int.class
中的for循环分配每个单元格时,它会显示NullPointerException。那么我在for循环中怎么做呢?
这是方法:
public MethodCall(String className,
String methodName,
Class[] paramTypes,
Object[] params) {
this.className = className;
this.methodName = methodName;
this.paramTypes = paramTypes;
this.params = params;
}
这是实例:
MethodCall methodCall = new MethodCall("Foo", "bar", new Class[]{int.class,double.class},new Object[]{new Integer(10), new Double(123.0)});
答案 0 :(得分:0)
您必须先初始化阵列。
paramTypes = new Class[4]; // or some other number
for(...){
paramTypes[i] = int.class;
}