0

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

Satish Rongala
  • 209
  • 6
  • 19
  • 1
    `java.lang.NullPointerException at lib.FunctionLibrary.TestCaseExecutor(FunctionLibrary.java:68)` What code is in `FunctionLibrary.java` code line `68`? – Axel Richter Jul 26 '18 at 09:54
  • 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()); } – Satish Rongala Jul 26 '18 at 09:58
  • initializing Method class and invoking the method... error is getting at method.invoke – Satish Rongala Jul 26 '18 at 10:01
  • 3
    Then either `TestData.sheet.getRow(Tests).getCell(5)` or `TestData.sheet.getRow(Tests).getCell(6)` or .`TestData.sheet.getRow(Tests).getCell(4)` returns `null` because [If you ask for a cell that is not defined....you get a null.](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.html#getCell-int-). You have to check if you really got a cell before using it or to use a appropriate [MissingCellPolicy](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.MissingCellPolicy.html). – Axel Richter Jul 26 '18 at 10:03
  • Ohh Okk Richter. i will check that once but my doubt is when try to run the TestCase1_Login it is running with out any error...only at the time of running the 2nd one getting the issue – Satish Rongala Jul 26 '18 at 10:05
  • Hi Richter.. now it is working fine.. but i have doubt why it is running fine with out any issue even though it is empty and why it is throwing error for 2nd one for leaving cells empty – Satish Rongala Jul 26 '18 at 10:18
  • A cell which looks empty in `Excel`'s GUI can either be present in the sheet but not filled with content. Then `Row.getCell` will return a blank cell whose string cell value is "". Or it can not even be present in the sheet. Then `Row.getCell` will return `null`. – Axel Richter Jul 26 '18 at 10:25
  • so how to resolve this.. filling the cell with data is the only solution to resolve this – Satish Rongala Jul 26 '18 at 10:32
  • "filling the cell with data is the only solution to resolve this": No, checking whether we got a `null` instead of a `Cell` is the solution. Or using a `MissingCellPolicy` while we get the `Cell` is another solution. Or using the methods of [org.apache.poi.ss.util.CellUtil](https://poi.apache.org/apidocs/org/apache/poi/ss/util/CellUtil.html) may be a third solution. – Axel Richter Jul 26 '18 at 10:37
  • 1
    Btw.: [Checking whether we got a null instead of an Object](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) is the solution for all `NullPointerException`s. – Axel Richter Jul 26 '18 at 10:41
  • Okk Richter.. Thank you.. i got the required answer..i need to set one validation whether getting value is null or data.. if it is null we have to take action accordingly :) – Satish Rongala Jul 26 '18 at 10:53

0 Answers0