Doubt about method in Java

Asked

Viewed 59 times

0

Would someone like to tell me how I pass the value of a parameter through the "invoke" method in Java?

I have a method that takes as parameter a String (This is the method I want to invoke).

However I am using a generic method that takes as parameter a class, and returns a String.

Briefly, I’m doing this:

Method mt = classe.getMethod("getSQL");
    Object invoke = mt.invoke(classe, filtro);

The parameter "class" is the class from which I want to find the method, and the "filter" is the parameter I would like to send to the method I want to invoke.

However I am getting the following error:

java.lang.NoSuchMethodException: br.com.viasoft.orion.model.evolucaovendas.EvolucaoVendas.getSQL()

If anyone can help, I’m grateful.

  • https://www.baeldung.com/java-method-reflection

  • Ball show the material, helped here. VLW!!

  • Check method name in class?

  • Yes, the problem was juxtaposing the parameter, because before when I had no parameter, I could without any problems. But now it worked, vlw

1 answer

2


According to the indication of a material about Java Reflection, forwarded via comment, I discovered the problem that was occurring.

Along those lines: Method mt = classe.getMethod("getSQL");, it was necessary to put the type of parameter that the method expects, thus:Method mt = classe.getMethod("getSQL, String.class");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.