jeudi 29 mars 2018

java.lang.IllegalArgumentException: object is not an instance of declaring class Getting this error in Method.invoke

CODE:- public static void CallMethod(String Keyword, String Object, String Data) { Method ReturnMethod = null; try { Package pkg = Package.getPackage("POM"); String pkgname = pkg.getName(); URL resource = ClassLoader.getSystemClassLoader().getResource(pkgname); if (resource == null) { throw new RuntimeException("No resource for " + pkgname); } File directory = new File(resource.toURI()); if (directory != null && directory.exists()) { String[] files = directory.list(); for (int i = 0; i < files.length; i++) { if (files[i].endsWith(".class")) { String className = pkgname+'.'+files[i].substring(0,files[i].length() - 6); System.out.println(Class.forName(className)); Class aClass = Class.forName(className); Method[] methods = aClass.getDeclaredMethods(); for (Method method : methods) { if (Keyword.equalsIgnoreCase(method.getName())) { Object obj[] = new Object[2]; obj[0] = Object; obj[1] = Data; method.invoke(aClass, "", obj); } } } } } } catch (Exception e) { e.printStackTrace(); } } CONSOLE:- java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at POM.Reflection.CallMethod(Reflection.java:35) at regression.Keywords.executekeywords(Keywords.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod (MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods (TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)





Aucun commentaire:

Enregistrer un commentaire