RefelctionAPI error message | Selenium Forum
M
Posted on 15/04/2016
Hi,

I am getting below error message: Whereas variable name and function name is same. When variable name and function name is sampleTest then code works fine.

Exception in thread "main" java.lang.NoSuchMethodException: ReflectionAPI.sample(java.lang.String)
at java.lang.Class.getMethod(Unknown Source)
at ReflectionAPIOne.main(ReflectionAPIOne.java:12)

[b:8632dhs6]Code is given below:[/b:8632dhs6]
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionAPIOne {



public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

String x = "sample";
//Method method = ReflectionAPI.class.getMethod(x, String.class);
Method method = ReflectionAPI.class.getMethod(x, String.class);
method.invoke(method, "age");
System.out.println("End");
}
public static void sample(String a){
System.out.println("in test " + a);
}
}

M
Replied on 16/04/2016

use this

package com.sample;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class ReflectionAPIOne {

public static void main(String[] args) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {

String x = "sample";
// Method method = ReflectionAPI.class.getMethod(x, String.class);
Method method = ReflectionAPIOne.class.getMethod(x, String.class);
method.invoke(method, "age");
System.out.println("End");
}

public static void sample(String a) {
System.out.println("in test " + a);
}
}


M
Replied on 18/04/2016

Thank you !!