英文:
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
问题
这是您提供的代码中的翻译部分:
我面临着这个问题,我正在尝试从一个类中循环所有我的方法到另一个类中,我已经做到了,但是我有两个循环,一个从结尾到开头执行,然后另一个没有执行。这个错误显示给我
`Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments`
这是我尝试获取我的方法结果的地方
public class finalResult {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
loopUserDataClass();
loopBankDataClass();
}
public static void loopUserDataClass() throws InvocationTargetException, IllegalAccessException {
userData us = new userData();
Class<?> getUserDataClass = us.getClass();
Method[] methods1 = getUserDataClass.getMethods();
for (Method method : methods1) {
if (Modifier.isPublic(method.getModifiers())) {
method.invoke(us, null);
}
}
}
public static void loopBankDataClass() throws InvocationTargetException, IllegalAccessException {
bankData bd = new bankData();
Class<?> getBankClass = bd.getClass();
Method[] methods = getBankClass.getMethods();
for (Method method : methods) {
if (Modifier.isPublic(method.getModifiers())) {
try {
method.invoke(bd, null);
} catch (Exception ex) {
// 在这里处理异常
ex.printStackTrace();
}
}
}
}
}
这是我拥有我的方法的类。
public class bankData {
public double moneyOnAccount = 123.50;
public String currencyName = "currencyName";
public boolean accountState = true;
public String accountId ="2432432423ffsdf";
public void getMoneyOnAccount() {
System.out.println("Money on account: " + this.moneyOnAccount);
}
public void getCurrencyName()
{
System.out.println("Currency: " + this.currencyName);
}
public void getAccountState()
{
System.out.println("Account statement: " + this.accountState);
}
public void getAccountId()
{
System.out.println("Account ID: " + this.accountId);
}
}
我相信我在这一行中有问题
```java
method.invoke(bd, null);
我尝试以许多不同的方式放置它,但总是显示相同的错误。我没有要传递的任何参数,所以我不知道我必须传递什么。
<details>
<summary>英文:</summary>
I am facing this problem, I am trying to loop all my methods from one class to another and I did do that, but I have 2 loops where one gets executed from the end to the beginning and then the other one does not get executed. This error shows to me
`Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments`
This is where I am trying to get the result of my methods
public class finalResult {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
loopUserDataClass();
loopBankDataClass();
}
public static void loopUserDataClass() throws InvocationTargetException, IllegalAccessException {
userData us = new userData();
Class<?> getUserDataClass = us.getClass();
Method[] methods1 = getUserDataClass.getMethods();
for (Method method : methods1) {
if (Modifier.isPublic(method.getModifiers())) {
method.invoke(us, null);
}
}
}
public static void loopBankDataClass() throws InvocationTargetException, IllegalAccessException {
bankData bd = new bankData();
Class<?> getBankClass = bd.getClass();
Method[] methods = getBankClass.getMethods();
for (Method method : methods) {
if (Modifier.isPublic(method.getModifiers())) {
try {
method.invoke(bd, null);
} catch (Exception ex) {
// Handle the exception here
ex.printStackTrace();
}
}
}
}
}
And here is the class where I have my methods.
public class bankData {
public double moneyOnAccount = 123.50;
public String currencyName = "currencyName";
public boolean accountState = true;
public String accountId ="2432432423ffsdf";
public void getMoneyOnAccount() {
System.out.println("Money on account: " + this.moneyOnAccount);
}
public void getCurrencyName()
{
System.out.println("Currency: " + this.currencyName);
}
public void getAccountState()
{
System.out.println("Account statement: " + this.accountState);
}
public void getAccountId()
{
System.out.println("Account ID: " + this.accountId);
}
}
I believe that I have a problem with this line
method.invoke(bd, null);
I tried to put it in too many different ways but always shows me the same error. I do not have any arguments to pass so I do not know what I have to pass there.
</details>
# 答案1
**得分**: 0
翻译结果如下:
所以,如果我稍微修改你的代码来执行...
try {
method.invoke(bd, null);
} catch (Exception ex) {
// 在这里处理异常
//ex.printStackTrace();
System.out.println(">>" + method.getName());
}
我们可以了解哪些方法导致了问题。在我的测试中,这会打印
>> wait
>> wait
>> wait
>> equals
>> notify
>> notifyAll
好的,这其实很有道理,所有这些方法都会期望一个或多个参数。
那么,我们该如何修复呢?嗯,不要使用 `Method[] methods = getBankClass.getMethods();`,你应该使用 `Method[] methods = getBankClass.getDeclaredMethods();`。
这两者之间的区别可以在[Java文档](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Class.html)中找到:
`Class#getMethods`:
> 返回一个包含反映由此 Class 对象表示的类或接口的所有公共方法的 Method 对象数组,包括由该类或接口声明的方法以及从超类和超接口继承的方法。
`Class#getDeclaredMethods`
> 返回一个包含反映由此 Class 对象表示的类或接口的所有已声明方法的 Method 对象数组,包括公共方法、受保护方法、默认(包)访问方法和私有方法,但不包括继承的方法。
<details>
<summary>英文:</summary>
So, if I modify your code slightly to do...
try {
method.invoke(bd, null);
} catch (Exception ex) {
// Handle the exception here
//ex.printStackTrace();
System.out.println(" >> " + method.getName());
}
We can get an idea of which methods are causing your issues. In my testing this prints
>> wait
>> wait
>> wait
>> equals
>> notify
>> notifyAll
Okay, that actually makes sense, all of those methods will expect one or more parameters.
So, how do we fix it? Well, instead of using `Method[] methods = getBankClass.getMethods();` you should probably be using `Method[] methods = getBankClass. getDeclaredMethods();`
The difference between the two can be found in the [JavaDocs](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Class.html)
`Class#getMethods`:
> Returns an array containing `Method` objects reflecting all the public methods of the class or interface represented by this Class object, **including those declared by the class or interface and those inherited from superclasses and superinterfaces.**
`Class#getDeclaredMethods`
> Returns an array containing `Method` objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, **but excluding inherited methods.**
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论