簡體   English   中英

Java參數數量錯誤

[英]Java wrong number of arguments

當運行此方法時,我得到錯誤的參數異常數,如下所示:

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)

例外是在注釋行中,盡管輸入的參數正確,但這是我的代碼:

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

我可以避免該異常並使它正常工作,但是正如您看到的那樣,對於一個作為參數傳遞的文件,這是代碼:

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

遠程調用此方法,因此我必須為作為參數傳遞的每個文件創建一個新文件,已知該文件是預先接收的。 問題是,當傳遞多個文件作為參數時,在這種情況下,如何創建文件列表,其中每個文件的ID都等於i然后瀏覽該列表? 我試圖像上面那樣使用HashMap做到這一點,但我不斷收到異常!

我通過替換來解決了這個問題:

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

由:

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