How do I fix a NoSuchMethodError?

前端 未结 29 2929
孤独总比滥情好
孤独总比滥情好 2020-11-21 05:08

I\'m getting a NoSuchMethodError error when running my Java program. What\'s wrong and how do I fix it?

29条回答
  •  礼貌的吻别
    2020-11-21 05:44

    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.)

提交回复
热议问题