简体   繁体   English

如何为类数组对象分配值?

[英]How to assign value for a class array object?

I am modifying a program, and it has Class[] paramTypes to store the input type. 我正在修改程序,它具有Class[] paramTypes来存储输入类型。 Then my question is how can I assign value for each cell of the array? 然后我的问题是如何为数组的每个单元分配值? I can do this paramTypes = new Class[]{int.class,double.class,String.class} to assign for the paramTypes, but when I try to assign each cell in for loop like paramTypes[i] = int.class , it shows NullPointerException. 我可以这样做paramTypes = new Class[]{int.class,double.class,String.class}来分配paramTypes,但是当我尝试在for循环中分配每个单元格时,如paramTypes[i] = int.class ,它显示NullPointerException。 So how shoud I do this in for loop? 那么我应该在for循环中这样做吗?

This is the method: 这是方法:

    public MethodCall(String className,
        String methodName,
        Class[] paramTypes,
        Object[] params) {
    this.className = className;
    this.methodName = methodName;
    this.paramTypes = paramTypes;
    this.params = params;
}

This is instance: 这是实例:

MethodCall methodCall = new MethodCall("Foo", "bar", new Class[]{int.class,double.class},new Object[]{new Integer(10), new Double(123.0)});

You have to initialize the array first. 您必须先初始化数组。

paramTypes = new Class[4]; // or some other number
for(...){
  paramTypes[i] = int.class;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM