java - Java错误的参数数量
问题描述
运行此方法时,我得到错误数量的参数异常,如下所示:
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
在顶部使用 like 来执行此操作,但我不断收到异常!
解决方案
我通过替换解决了这个问题:
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()));
推荐阅读
- javascript - Node js MongoDB登录系统数据库值未定义
- javascript - 用 npm start 启动我的机器人不起作用!不和谐.js
- regex - 雪花正则表达式语法问题
- php - 为什么数据库的“id”列的值显示为空?
- javascript - 在 promise 或 observable 中,resolve 或subscriber 的真正含义是什么?
- python - 使用子进程从另一个驱动器调用程序时出现权限错误
- c# - 当我在 Azure VM 中尝试使用带有 Database1.mdf 文件的 EF Core 控制台应用程序时,它不起作用
- gcc - gccmakedep 错误:#error 此文件需要对 ISO C++ 2011 标准的编译器和库支持
- python - 如何对数据框中的多个列进行分组?
- vue.js - “TypeError:无法读取未定义的属性'get'”,Axios,Vue.JS