I\'m getting a NoSuchMethodError
error when running my Java program. What\'s wrong and how do I fix it?
This can also be the result of using reflection. If you have code that reflects on a class and extracts a method by name (eg: with Class.getDeclaredMethod("someMethodName", .....)
) then any time that method name changes, such as during a refactor, you will need to remember to update the parameters to the reflection method to match the new method signature, or the getDeclaredMethod
call will throw a NoSuchMethodException
.
If this is the reason, then the stack trace should show the point that the reflection method is invoked, and you'll just need to update the parameters to match the actual method signature.
In my experience, this comes up occasionally when unit testing private methods/fields, and using a TestUtilities
class to extract fields for test verification. (Generally with legacy code that wasn't designed with unit testing in mind.)