
[英]Exception in thread “main” java.lang.IllegalArgumentException: wrong number of arguments while running kafka producer
[英]java.lang.IllegalArgumentException: wrong number of arguments Exception while invoking constructor
这是我正在使用的代码,它在运行时使用构造函数提供的参数实例化并创建对象。
private static void createInstancesFromSpecfication() {
String[] specifierFileNames = resourceMap.get("instances");
if (null == specifierFileNames)
return;
List<SpecifierObjects> instances = new ArrayList<SpecifierObjects>();
for (int i = 0; i < specifierFileNames.length; i++) {
try {
instances.addAll(XMLFileUtility.readXML(specifierFileNames[i],
new ClassInstantiationHandler()));
} catch (ResourceNotAvailableException | DocumentException
| ResourceDataFailureException e) {
e.printStackTrace();
}
}
for (Iterator<SpecifierObjects> iterator = instances.iterator(); iterator
.hasNext();) {
InstanceSpecifierObject instanceSpecifierObject = (InstanceSpecifierObject) iterator
.next();
try {
Class<?> cast = Class.forName(instanceSpecifierObject
.getInterfaceName());
Class<?> clazz = Class.forName(instanceSpecifierObject
.getClassName());
Constructor<?> constructor = clazz
.getConstructor(String[].class);
Object obj = constructor.newInstance(instanceSpecifierObject
.getFilesName());
objects.put(instanceSpecifierObject.getObjectName(),
cast.cast(obj));
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
e.printStackTrace();
}
}
}
异常日志为:
instances=/home/user/workspace/CoreLibs/src/instanceSpecification.xml
/home/user/workspace/CoreLibs/src/instanceSpecification.xml
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.framew.confinit.ApplicationResource.createInstancesFromSpecfication(ApplicationResource.java:87)
at com.framew.confinit.ApplicationResource.importResource(ApplicationResource.java:58)
at com.framew.confinit.ApplicationResource.main(ApplicationResource.java:40)
我在以下语句中得到了例外:
Object obj = constructor.newInstance(instanceSpecifierObject.getFilesName());
instanceSpecifierObject.getFilesNames()将文件名数组重新运行为String对象(例如String [])。 我也尝试将它们作为Object []传递,但是遇到了同样的异常。
任何帮助表示赞赏。 提前致谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.