简体   繁体   中英

How to assign value for a class array object?

I am modifying a program, and it has Class[] paramTypes to store the input type. 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. So how shoud I do this in for loop?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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