Java反射:“java.lang.NoSuchMethodException” [英] Java Reflection: "java.lang.NoSuchMethodException"

查看:829
本文介绍了Java反射:“java.lang.NoSuchMethodException”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the method from another class using reflection, but for some reason it keeps on giving me a no such method exception. These are the classes im using:

ScriptTable类:

ScriptTable class:

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

DummyScript类

DummyScript class

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


推荐答案

这是你的问题:

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

这篇关于Java反射:“java.lang.NoSuchMethodException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆