“Exception in thread ‘main’ java.lang.IllegalArgumentException: wrong number of arguments”

huangapple go评论136阅读模式
英文:

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments

问题

这是您提供的代码中的翻译部分:

  1. 我面临着这个问题我正在尝试从一个类中循环所有我的方法到另一个类中我已经做到了但是我有两个循环一个从结尾到开头执行然后另一个没有执行这个错误显示给我
  2. `Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments`
  3. 这是我尝试获取我的方法结果的地方
  4. public class finalResult {
  5. public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
  6. loopUserDataClass();
  7. loopBankDataClass();
  8. }
  9. public static void loopUserDataClass() throws InvocationTargetException, IllegalAccessException {
  10. userData us = new userData();
  11. Class<?> getUserDataClass = us.getClass();
  12. Method[] methods1 = getUserDataClass.getMethods();
  13. for (Method method : methods1) {
  14. if (Modifier.isPublic(method.getModifiers())) {
  15. method.invoke(us, null);
  16. }
  17. }
  18. }
  19. public static void loopBankDataClass() throws InvocationTargetException, IllegalAccessException {
  20. bankData bd = new bankData();
  21. Class<?> getBankClass = bd.getClass();
  22. Method[] methods = getBankClass.getMethods();
  23. for (Method method : methods) {
  24. if (Modifier.isPublic(method.getModifiers())) {
  25. try {
  26. method.invoke(bd, null);
  27. } catch (Exception ex) {
  28. // 在这里处理异常
  29. ex.printStackTrace();
  30. }
  31. }
  32. }
  33. }
  34. }
  35. 这是我拥有我的方法的类
  36. public class bankData {
  37. public double moneyOnAccount = 123.50;
  38. public String currencyName = &quot;currencyName&quot;;
  39. public boolean accountState = true;
  40. public String accountId =&quot;2432432423ffsdf&quot;;
  41. public void getMoneyOnAccount() {
  42. System.out.println(&quot;Money on account: &quot; + this.moneyOnAccount);
  43. }
  44. public void getCurrencyName()
  45. {
  46. System.out.println(&quot;Currency: &quot; + this.currencyName);
  47. }
  48. public void getAccountState()
  49. {
  50. System.out.println(&quot;Account statement: &quot; + this.accountState);
  51. }
  52. public void getAccountId()
  53. {
  54. System.out.println(&quot;Account ID: &quot; + this.accountId);
  55. }
  56. }
  57. 我相信我在这一行中有问题
  58. ```java
  59. method.invoke(bd, null);

我尝试以许多不同的方式放置它,但总是显示相同的错误。我没有要传递的任何参数,所以我不知道我必须传递什么。

  1. <details>
  2. <summary>英文:</summary>
  3. 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
  4. `Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: wrong number of arguments`
  5. 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();
}

  1. public static void loopUserDataClass() throws InvocationTargetException, IllegalAccessException {
  2. userData us = new userData();
  3. Class&lt;?&gt; getUserDataClass = us.getClass();
  4. Method[] methods1 = getUserDataClass.getMethods();
  5. for (Method method : methods1) {
  6. if (Modifier.isPublic(method.getModifiers())) {
  7. method.invoke(us, null);
  8. }
  9. }
  10. }
  11. public static void loopBankDataClass() throws InvocationTargetException, IllegalAccessException {
  12. bankData bd = new bankData();
  13. Class&lt;?&gt; getBankClass = bd.getClass();
  14. Method[] methods = getBankClass.getMethods();
  15. for (Method method : methods) {
  16. if (Modifier.isPublic(method.getModifiers())) {
  17. try {
  18. method.invoke(bd, null);
  19. } catch (Exception ex) {
  20. // Handle the exception here
  21. ex.printStackTrace();
  22. }
  23. }
  24. }
  25. }
  26. }
  1. 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);
}
}

  1. I believe that I have a problem with this line

method.invoke(bd, null);

  1. 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.
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 翻译结果如下:
  6. 所以,如果我稍微修改你的代码来执行...
  7. try {
  8. method.invoke(bd, null);
  9. } catch (Exception ex) {
  10. // 在这里处理异常
  11. //ex.printStackTrace();
  12. System.out.println("&gt;&gt;" + method.getName());
  13. }
  14. 我们可以了解哪些方法导致了问题。在我的测试中,这会打印
  15. &gt;&gt; wait
  16. &gt;&gt; wait
  17. &gt;&gt; wait
  18. &gt;&gt; equals
  19. &gt;&gt; notify
  20. &gt;&gt; notifyAll
  21. 好的,这其实很有道理,所有这些方法都会期望一个或多个参数。
  22. 那么,我们该如何修复呢?嗯,不要使用 `Method[] methods = getBankClass.getMethods();`,你应该使用 `Method[] methods = getBankClass.getDeclaredMethods();`
  23. 这两者之间的区别可以在[Java文档](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Class.html)中找到:
  24. `Class#getMethods`
  25. &gt; 返回一个包含反映由此 Class 对象表示的类或接口的所有公共方法的 Method 对象数组,包括由该类或接口声明的方法以及从超类和超接口继承的方法。
  26. `Class#getDeclaredMethods`
  27. &gt; 返回一个包含反映由此 Class 对象表示的类或接口的所有已声明方法的 Method 对象数组,包括公共方法、受保护方法、默认(包)访问方法和私有方法,但不包括继承的方法。
  28. <details>
  29. <summary>英文:</summary>
  30. So, if I modify your code slightly to do...
  31. try {
  32. method.invoke(bd, null);
  33. } catch (Exception ex) {
  34. // Handle the exception here
  35. //ex.printStackTrace();
  36. System.out.println(&quot; &gt;&gt; &quot; + method.getName());
  37. }
  38. We can get an idea of which methods are causing your issues. In my testing this prints
  39. &gt;&gt; wait
  40. &gt;&gt; wait
  41. &gt;&gt; wait
  42. &gt;&gt; equals
  43. &gt;&gt; notify
  44. &gt;&gt; notifyAll
  45. Okay, that actually makes sense, all of those methods will expect one or more parameters.
  46. So, how do we fix it? Well, instead of using `Method[] methods = getBankClass.getMethods();` you should probably be using `Method[] methods = getBankClass. getDeclaredMethods();`
  47. 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)
  48. `Class#getMethods`:
  49. &gt; 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.**
  50. `Class#getDeclaredMethods`
  51. &gt; 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.**
  52. </details>

huangapple
  • 本文由 发表于 2023年2月27日 06:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定