
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - NoSuchMethodException
The Java NoSuchMethodException is thrown when a particular method is not found in a class. The NoSuchMethodException is a checked exception that is thrown when a particular method is not found in a class.
It extends ReflectiveOperationException, which is the superclass for exceptions thrown by reflection-related operations.
Hierarchy of NoSuchMethodException
Given below is the class hierarchy of NoSuchMethodException:
java.lang.Object â³ java.lang.Throwable â³ java.lang.Exception â³ java.lang.ReflectiveOperationException â³ java.lang.NoSuchMethodException
So, it means ReflectiveOperationException is the superclass for exceptions thrown by reflection-related operations and it is unchecked.
Cause of NoSuchMethodException
Following are the reasons when JVM throws a NoSuchMethodException in Java:
- A method that does not exist is accessed via reflection.
- A method exists but is private and is accessed using getMethod() instead of getDeclaredMethod().
- A misspelled method name is used.
- The class being inspected is dynamically loaded but does not contain the expected method.
Constructors of NoSuchMethodException
There are two constructors of NoSuchMethodException class:
- NoSuchMethodException(): This constructor is used to create a NoSuchMethodException object without any message.
- NoSuchMethodException(String message): This constructor is used to create a NoSuchMethodException object with a message.
Methods of NoSuchMethodException
There are some methods of NoSuchMethodException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example 1 : Accessing a Non-Existent Method
In this example, we are accessing a method that does not exist in the class, so JVM will throw a NoSuchMethodException.
import java.lang.reflect.Method; public class NoSuchMethodExample { public static void main(String[] args) { Class<?> clazz = Class.forName("java.lang.String"); Method method = clazz.getMethod("nonExistentMethod"); // This method does not exist } }
Output
Following is the output of the above code:
NoSuchMethodExample.java:6: error: unreported exception NoSuchMethodException; must be caught or declared to be thrown Method method = clazz.getMethod("nonExistentMethod"); // This method does not exist ^ 2 errors
As you can see in the output, JVM throws a NoSuchMethodException because we are accessing a method that does not exist in the class.
Difference Between getMethod() and getDeclaredMethod()
There are two methods in the java.lang.Class class that are used to get the method of a class:
- getMethod(String name, Class<?>... parameterTypes): This method is used to get the public method of the class.
- getDeclaredMethod(String name, Class<?>... parameterTypes): This method is used to get the method of the class.
Following table shows the difference between getMethod() and getDeclaredMethod() methods:
Method | Visibility | Accesses Inherited Methods? |
---|---|---|
getMethod(String name, Class<?>... parameterTypes) | Only public | Yes |
getDeclaredMethod(String name, Class<?>... parameterTypes) | Any (private, protected, public) | No |
Example 2 : Accessing a Private Method Correctly
In this example, we are accessing a private method using getDeclaredMethod() method, so JVM will not throw a NoSuchMethodException.
import java.lang.reflect.Method; public class NoSuchMethodExample { public static void main(String[] args) { Class<?> clazz = Class.forName("java.lang.String"); Method method = clazz.getDeclaredMethod("nonExistentMethod"); // This method does not exist } }
Output
Following is the output of the above code:
NoSuchMethodExample.java:6: error: unreported exception NoSuchMethodException; must be caught or declared to be thrown Method method = clazz.getDeclaredMethod("nonExistentMethod"); // This method does not exist ^ 2 errors
As you can see in the output, JVM throws a NoSuchMethodException because we are accessing a method that does not exist in the class.
Handling NoSuchMethodException
In this example, we are accessing a method that does not exist in the class, so JVM will throw a NoSuchMethodException. We are handling this exception using a try-catch block.
import java.lang.reflect.Method; public class NoSuchMethodExample { public static void main(String[] args) { try { Class<?> clazz = Class.forName("java.lang.String"); Method method = clazz.getMethod("nonExistentMethod"); // This method does not exist } catch (NoSuchMethodException e) { System.out.println("Method does not exist"); } } }
Output
Following is the output of the above code:
Method does not exist
How to avoid NoSuchMethodException?
To avoid a NoSuchMethodException, you should always access the method of a class that exists in the class.
Let's try to access a method that exists in the class:
import java.lang.reflect.Method; public class NoSuchMethodExample { public static void main(String[] args) { try { Class<?> clazz = Class.forName("java.lang.String"); Method method = clazz.getMethod("length"); // This method exists System.out.println("Method exists"); } catch (NoSuchMethodException e) { System.out.println("Method does not exist"); } } }
Output
Following is the output of the above code:
Method exists
As you can see in the output, we are accessing a method that exists in the class, so JVM does not throw a NoSuchMethodException.
Summary
- The NoSuchMethodException in Java occurs when a particular method is not found in a class.
- Following are the reasons when JVM throws a NoSuchMethodException in Java:
- A method that does not exist is accessed via reflection.
- A method exists but is private and is accessed using getMethod() instead of getDeclaredMethod().
- A misspelled method name is used.
- The class being inspected is dynamically loaded but does not contain the expected method.