0

我正在修改一个程序,它必须Class[] paramTypes存储输入类型。那么我的问题是如何为数组的每个单元格赋值?我可以这样做paramTypes = new Class[]{int.class,double.class,String.class}来分配 paramTypes,但是当我尝试分配 for 循环中的每个单元格时paramTypes[i] = int.class,它显示 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)});
4

1 回答 1

0

You have to initialize the array first.

paramTypes = new Class[4]; // or some other number
for(...){
  paramTypes[i] = int.class;
}
于 2013-10-07T04:26:17.580 回答