如何调用字符串数组中的Java方法?(java.lang.NoSuchMethodException)

麦伯利

我有一个ArrayStringsMethod names我正努力在一个循环中调用。我已经检索了该类中所有变量的名称,并添加了所需的文本以填充整个函数,并将整个字符串添加到列表中:

String[] methodNames= new String[16];
Class c = telegramm.class;
Field[] fields = c.getDeclaredFields();

int x=0;
for (Field field : fields) 
{               
    String str = field.getName();
    String variableName = str.substring(0, 1).toUpperCase() + str.substring(1);
    // This is how I initially intended to call the method
    // methodNames[x] = "set" + variableName + "(parts[" + x + "]);";

    // but with the method I found I used this
    methodNames[x] = "set" + variableName";

    Method gs1Method = tClass.getMethod("getString1", new Class[] {});
    //String str1 = (String) gs1Method.invoke(t, new Object[] {});
    //System.out.println("getString1 returned: " + str1);
    x++;
}

因此,我将一个名为String的方法添加到数组中methodNames

而且我找到了一种调用方法的方法:

Method gs1Method = tClass.getMethod("getString1", new Class[] {});
String str1 = (String) gs1Method.invoke(t, new Object[] {});
System.out.println("getString1 returned: " + str1);

该方法有效,但是仅当您对方法名称(“ getString1”)进行硬编码时才有效。

现在,我想实现一种通过methodNames数组调用这些函数的方法

当我将methodNames数组的对象作为参数传递时:

getMethod(methodNames[x], new Class[] {});

我得到错误:

Exception in thread "main" java.lang.NoSuchMethodException: 
kin.gateway.adient.Testing.setBoolean1(java.lang.String)
at java.lang.Class.getMethod(Class.java:1786)
at kin.gateway.adient.ClassMethodTest.main(ClassMethodTest.java:31)

我还尝试将字符串添加到单个变量中,String variableName = "set" + variableName;但是得到了相同的结果。

为什么不接受String作为变量?

科特比·法鲁克(Korteby Farouk)

在您可以根据自己的方法在其上添加循环之后,尝试此示例应该可以帮助您找到错误:

1-您的电报课程:

public class Telegramm {

    String value1;
    String value2;

    public String getValue1() {
        return this.value1;
    }

    public void setValue1(String value) {
        this.value1 = value;
    }

    public String getValue2() {
        return this.value2;
    }

    public String setValue2(String value) {
        this.value2 = value;
        return this.value2;
    }

}

2-方法的调用:getValue1()和setValue2()

try {
            // Create our Telegramm instance
            Telegramm telegramm = new Telegramm();
            telegramm.setValue1("value1");
            telegramm.setValue2("value2");

            // Invoke public static String getValue1()
            Method getValue1Method = telegramm.getClass().getMethod("getValue1", null);
            String result = (String)getValue1Method.invoke(telegramm);
            System.out.println("result invocation getValue1() : " + result);

            // Invoke public static String setValue2()
            getValue1Method = telegramm.getClass().getMethod("setValue2", new Class[] {String.class});
            result = (String)getValue1Method.invoke(telegramm, "ValueX");
            System.out.println("result invocation setValue2() : " + result);

        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.lang.NoSuchMethodException: <init> []

Kotlin java.lang.NoSuchMethodException:<init>()

java.lang.NoSuchMethodException:AffirmativeBased。<init>()

java.lang.NoSuchMethodException为的onCreate

java.lang.NoSuchMethodException struts servlet

android java.lang.AssertionError:java.lang.NoSuchMethodException-Proguard

java.lang.RuntimeException:java.lang.NoSuchMethodException:Hadoop mapreduce

Scala案例类中的init方法的java.lang.NoSuchMethodException

Java反射错误Java.lang.NoSuchMethodException,但是方法存在

java.lang.NoSuchMethodException:Bean没有名为的属性

引起:java.lang.NoSuchMethodException Spring Batch xml配置

java.lang.NoSuchMethodException:userAuth.User。<init>()

java.lang.NoSuchMethodException:package.ClassName。<init>()使用Class <?>

java.lang.NoSuchMethodException:scala.collection.immutable。$ colon $ colon

尝试使用OncePerRequestFilter时发生java.lang.NoSuchMethodException

java.lang.NoSuchMethodException:akka.cluster.ClusterActorRefProvider

一个java.lang.NoSuchMethodException错误(布局)

错误:java.lang.NoSuchMethodException:Spring MVC中的java.lang.Long。<init>()

java.lang.NoSuchMethodException: <Class>.<init>(java.lang.String) 复制自定义 Transformer 时

获取java.lang.NoSuchMethodException:javafx.application.Platform.startup(java.lang.Runnable)

启动Servlet筛选器时如何解决java.lang.NoSuchMethodException:package.MyCustomFilter。<init>()

线程“主”中的异常java.lang.NoSuchMethodException:添加ArrayList之后

在构造函数中传递java.lang.Integer的参数时,发生NoSuchMethodException(反射)

Java反射:原始int的构造函数原因:java.lang.NoSuchMethodException:int。<init>(int)

java.lang.NoSuchMethodException:org.springframework.boot.autoconfigure.http.HttpMessageConverters

找不到默认的构造函数;嵌套异常是Spring MVC的java.lang.NoSuchMethodException?

使用基于代码的配置获取java.lang.NoSuchMethodException:org.apache.catalina.deploy.WebXml addServlet

无法在pyspark中加载已保存的Spark模型:“ java.lang.NoSuchMethodException”

自定义弹簧条件导致java.lang.NoSuchMethodException:<init>()异常