jeudi 26 juillet 2018

Getting NullpointErrorException in the time of running multiple methods when Using Java Reflection

Hi i tried to run the multiple methods using java reflection but it is running one method without any issue but when it starting the new method it is getting "NullPointException" even though the method existed in java class.

i'm trying to run the class"TestCase2_Login" mentioned in the attached image.. it is successfully running the code related to Login functionality but it is running the 'Cart_Manage' method and it is giving NullPointerException. i am not able to understand where it is throwing the NullPointerException because in the Debgging mode also all the code went fine.. but at invoke method it is throwing exception

//Executable code//

    public static void TestCaseExecutor(Class classname) throws ClassNotFoundException{

    LoadExcel TestData=new LoadExcel();
    FunctionLibrary lib=new FunctionLibrary(driver);
    for(int Tests=1;Tests<TestData.sheet.getLastRowNum()+1;Tests++)  //Getting TestName
    {
       String ClassName=classname.getSimpleName();
         String TestName=TestData.sheet.getRow(Tests).getCell(0).getStringCellValue().trim();
         if(ClassName.equals(TestName)) //Comparing TestNames from sheet
         {
             System.out.println(TestName+" Class Name");
          String MethodName=TestData.sheet.getRow(Tests).getCell(2).getStringCellValue().trim();  //Method Name from Excel
              System.out.println(MethodName+" Methodname");
          try{  
                int parameterCount=(int) TestData.sheet.getRow(Tests).getCell(3).getNumericCellValue();   //parameter count from excel

                for(Method m: FunctionLibrary.class.getMethods())    //reading Method names from Functional library
                {  
                    if(m.getName().equals(MethodName))  //Compapring Method Name with Excel method name
                    {
                        if(m.getParameterCount()==parameterCount)   //Comparing paraameter Count from both FL and Excel
                        {
                            Class<?> param[]=new Class[parameterCount]; //Creating an array with class

                            for(int i=0;i<m.getParameterCount();i++) 
                            {
                                param[i]=m.getParameterTypes()[i];  //assigning values in to array with parameter type

                            }
                            Method method=FunctionLibrary.class.getMethod(MethodName,param); 
                            method.invoke(lib,TestData.sheet.getRow(Tests).getCell(5).getStringCellValue(),TestData.sheet.getRow(Tests).getCell(6).getStringCellValue(),TestData.sheet.getRow(Tests).getCell(4).getStringCellValue());  
                        }
                }else if(m.getName().equals(""))
                {
                    System.out.println("empty method name");
                    break;
                }
                }
    }catch(InvocationTargetException | NoSuchMethodException | IllegalAccessException | NullPointerException  e){                               
            e.printStackTrace();
            assertTrue(false);
        }
      }
    }   
}

//Error Log//

java.lang.NullPointerException
at lib.FunctionLibrary.TestCaseExecutor(FunctionLibrary.java:68)
at testCaseWithKeywordFramework.TestCase2_Login.login(TestCase2_Login.java:19)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
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:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
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)

ExcelSheetImage Having Testdata and Method Click here





Aucun commentaire:

Enregistrer un commentaire