Javatpoint Logo

Custom Exception

By: rickys*** On: Fri Dec 20 09:19:34 IST 2013     Question Reputation5 Answer Reputation0 Quiz Belt Series Points0  5Blank User
Ques 1) When i am run the below program then i got the following exception

Exception in thread "main" java.lang.NoSuchMethodException: Hello.say hello()
at java.lang.Class.getMethod(Unknown Source)
at Test.main(Test.java:35)

tell me, what i mistake in the below program.


Prog -

// Creating annotation

import java.lang.annotation.*;
import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation
{
int value();
}


// Applying annotation

class Hello
{
@MyAnnotation(value = 10)
public void sayHello()
{
System.out.println("hello annotation");
}
}

// Accessing annotation

class Test
{
public static void main(String args[]) throws Exception
{
Hello h = new Hello();
Method m = h.getClass().getMethod("say hello");

MyAnnotation manno = m.getAnnotation(MyAnnotation.class);
System.out.println("value is : " + manno.value());
}
}
Up0Down

 
The exceptions which is thrown by JVM is very clear that
<<<<<<<<<
Exception in thread "main" java.lang.NoSuchMethodException: Hello.say hello()
>>>>>>>>>

The above exception is saying that "Hello" class does not have a method "say hello()". I think you have to do the following correction in your Test.java
<<<<<
Method m = h.getClass().getMethod("sayHello");
>>>>>

Image Created0Down

By: [email protected] On: Tue Dec 24 21:28:18 IST 2013 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :4Yes2No