[英]How to assign value for a class array object?
我正在修改程序,它具有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)});
您必須先初始化數組。
paramTypes = new Class[4]; // or some other number
for(...){
paramTypes[i] = int.class;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.