【问题标题】:Java Reflection: "java.lang.NoSuchMethodException"Java 反射:“java.lang.NoSuchMethodException”
【发布时间】:2013-11-09 15:12:15
【问题描述】:

我正在尝试使用反射从另一个类中获取方法,但由于某种原因,它一直给我一个没有这样的方法异常。这些是我使用的类:

ScriptTable 类:

for(Class<?> script: Scripts) {
        System.out.println(script.getName());
        try {
            Method c = script.getMethod("scriptInfo()", script);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

DummyScript 类

    public String[] scriptInfo() {
    String[] ScriptInfo = {"DummyScript", "Chris", "Does nothing atm"};
    return ScriptInfo;
}

【问题讨论】:

  • 如果您对代码进行采样而不是 pastebin 会更好。

标签: java class reflection methods


【解决方案1】:

这是你的问题:

script.getMethod("scriptInfo()", script);

改成:

script.getMethod("scriptInfo");

看看这里,看看为什么:

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2013-11-23
    • 1970-01-01
    • 2019-09-21
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2010-09-30
    相关资源
    最近更新 更多