繁体   English   中英

java.lang.IllegalArgumentException:参数个数错误调用构造函数时发生异常

[英]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 []传递,但是遇到了同样的异常。

任何帮助表示赞赏。 提前致谢。

  1. Class类的newInstance()方法可以调用零参数构造函数,而Constructor类的newInstance()方法可以调用任意数量的参数。 因此,构造类优于类。
  2. 尝试使用构造函数,如果不起作用,则编写一条for:each语句,打印出instanceSpecifierObject.getFilesName(),并确保它确实返回了Array。 我不明白为什么它不起作用。
    1. 我想为您提供更多帮助,但不要过多使用反思,这不是一个很好的实践,我需要查看您所有的课程

暂无
暂无

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

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