简体   繁体   English

Java参数数量错误

[英]Java wrong number of arguments

When runing this method i get wrong number of arguments exception like this : 当运行此方法时,我得到错误的参数异常数,如下所示:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javafxcyberwind.Participant_MethodsIMPL.execution(Participant_MethodsIMPL.java:85)

The exception is in the comment line, despite the arguments entered are correct, this is my code: 例外是在注释行中,尽管输入的参数正确,但这是我的代码:

 @Override
 public void execution(String cls, String ip, Object... par) throws InvocationTargetException, RemoteException {
        try {
            URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file:///" + prep)});
            Class<?> c = loader.loadClass(cls);
            Object j = c.newInstance();
            Method[] methods = c.getDeclaredMethods();
            for (Method method : methods) {
                ArrayList<Object> tab = new ArrayList<>();
                if (method.getReturnType() == int.class || method.getReturnType() == String.class || method.getReturnType() == boolean.class || method.getReturnType() == double.class) {
                tab.clear();
                tab.addAll(Arrays.asList(par));
                int i = 0;                    
                HashMap<Integer, File> lif = new HashMap<>();
                File file = null;
                for (Object o : tab) {
                    if (o.getClass().equals(Fichier.class)) {
                        String nomfichier = ((Fichier) o).getNom();
                        file = new File(prep + nomfichier);                            
                        lif.put(i, file);
                    }
                    i++;
                }                 
                for (Map.Entry<Integer, File> entry : lif.entrySet()) {
                    tab.remove(entry.getKey());
                    tab.add(entry.getKey(), entry.getValue());
                }
                k = method.invoke(j, tab.toArray());//line of exception
                if (file != null) {
                    file.delete();
                }
            }
                if (method.getReturnType().toString().equals("class java.io.File")) {
                    tab.clear();
                    tab.addAll(Arrays.asList(par));
                    int i = 0;
                    int t = -1;
                    String nomfichier = null;
                    File file = null;
                    for (Object o : tab) {
                        if (o.getClass().equals(Fichier.class)) {
                            nomfichier = ((Fichier) o).getNom();
                            file = new File(prep + nomfichier);
                            t = i;
                        }
                        i++;
                    }
                    if (t != -1) {
                        tab.remove(t);
                        tab.add(t, file);
                    }
                    k = method.invoke(j, tab.toArray());
                    if (file != null) {
                        file.delete();
                    }
                    fff = nomfichier.replace(nomfichier, cls + "_" + nomfichier);
                    File fres = new File(prep + fff);
                    R.uploadToCloud(fff);
                    Socket s = new Socket(ip, R.getPort());
                    FileInputStream inf = new FileInputStream(fres);
                    ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
                    byte buf[] = new byte[1024];
                    int n;
                    while ((n = inf.read(buf)) != -1) {
                        out.write(buf, 0, n);
                    }
                    out.close();
                    inf.close();
                    s.close();
                    fres.delete();
                }
            }
        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(Participant_MethodsIMPL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

I can avoid the exception and make it work but as you see just for one file passed as an argument, this is the code : 我可以避免该异常并使它正常工作,但是正如您看到的那样,对于一个作为参数传递的文件,这是代码:

if (method.getReturnType() == int.class || method.getReturnType() == String.class || method.getReturnType() == boolean.class || method.getReturnType() == double.class) {
                tab.clear();
                tab.addAll(Arrays.asList(par));
                int i = 0;
                int t = -1;
                File file = null;
                for (Object o : tab) {
                    if (o.getClass().equals(Fichier.class)) {//means there is an argument of type File
                        String nomfichier = ((Fichier) o).getNom();//getting the file name
                        file = new File(prep + nomfichier);//file that will replace the remote file
                        t = i;
                    }
                    i++;
                }
                if (t != -1) {//replacing the remote file
                    tab.remove(t);
                    tab.add(t, file);
                }
                k = method.invoke(j, tab.toArray());
                if (file != null) {
                    file.delete();
                }
            }

This method is called remotely so i have to create a new file for each file passed as an argument known that the files are received in advance. 远程调用此方法,因此我必须为作为参数传递的每个文件创建一个新文件,已知该文件是预先接收的。 The problem is when passing more than one file as an argument, in this case, how can i create a list of files where each file have an id that equals i then just browse this list ? 问题是,当传递多个文件作为参数时,在这种情况下,如何创建文件列表,其中每个文件的ID都等于i然后浏览该列表? I tried to do this with HashMap like on top but i keep getting the exception ! 我试图像上面那样使用HashMap做到这一点,但我不断收到异常!

I solved this by just replacing : 我通过替换来解决了这个问题:

 for (Map.Entry<Integer, File> entry : lif.entrySet()) {
    tab.remove(entry.getKey());
    tab.add(entry.getKey(), entry.getValue());
 }

By : 由:

lif.entrySet().stream().forEach(entry -> tab.set(entry.getKey(), entry.getValue()));

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

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