1

I get a NoSuchMethodException when executing:

operacionDTO.getClass().getMethod("setPrioridad").invoke(operacionDTO, 0);

java.lang.NoSuchMethodException: xxxx.api.service.dto.RegasificacionDTO.setPrioridad()

But class RegasificacionDTO does have a public method called setPrioridad(int i), and if when debugging I call:

operacionDTO.getClass().getMethods()

Then I get an array of Method in which there is a setPrioridad. I've tried with some other similar methods and I get the same error.

3 Answers 3

11

You need to include the parameter signature.

 operacionDTO.getClass().getMethod("setPrioridad", Integer.TYPE)
Sign up to request clarification or add additional context in comments.

Comments

4

Method getMethod() accepts method name and an varargs array of parameter types. In your case you should call getMethod("setPrioridad", int.class) and everything will work.

This is because in java (as in most object oriented languages) you can define several methods with the same name and different signatures, so the system distinguishes among them using given parameter types.

Comments

1
 operacionDTO.getClass().getMethod("setPrioridad",new Class[]{Integer.TYPE or Integer.class}).invoke(operacionDTO, 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.