Satish Rongala
Satish Rongala

Reputation: 229

Getting NullPointerException while running script with Testng xml but script is working fine when running with "Running Testng Programatically"

i'm facing one different issue that when i'm running my script with Testng xml, it is throwing null point error but when i'm running script with "running Testng Programatically" it is working fine with out any issue.

Code

public void TestCaseExecutor(Class classname) throws ClassNotFoundException {

    FunctionLibrary lib = new FunctionLibrary(driver);

    // Getting TestName
    for (int Tests = 1; Tests < TestData.sheet.getLastRowNum() + 1; Tests++) {
        String ClassName = classname.getSimpleName();

        String TestName = TestData.sheet
                            .getRow(Tests)
                            .getCell(0)
                            .getStringCellValue()
                            .trim();

        // Comparing TestNames from sheet
        if (ClassName.equals(TestName)) {
            // Method Name from Excel
            String MethodName = TestData.sheet
                                    .getRow(Tests)
                                    .getCell(2)
                                    .getStringCellValue()
                                    .trim();
            try {
                // parameter count from excel
                int parameterCount = (int) TestData.sheet
                                            .getRow(Tests)
                                            .getCell(3)
                                            .getNumericCellValue();

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

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

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

calling the above function in my script

public class TestCase3_Register extends Browser_nav{

    @Test
    public void register() {
        FunctionLibrary obj = new FunctionLibrary(driver);

        try {
            obj.TestCaseExecutor(this.getClass());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 
    }
}

My Excel Class

public class LoadExcel {

    public static int rowCount;
    public static XSSFSheet sheet;
    public static XSSFWorkbook book;
    public static void loadExcelSheet(){
        try{

        File file=new File("C:\\Users\\DELL\\Desktop\\TestData.xlsx");
        FileInputStream fis=new FileInputStream(file);
        try {
            book=new XSSFWorkbook(file);
            sheet=book.getSheet("TestSteps");
            rowCount=sheet.getLastRowNum();
        } catch (InvalidFormatException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
    }

}

Error log:

java.lang.NullPointerException
at lib.FunctionLibrary.TestCaseExecutor(Functionlibrary.java:50)
at testCaseWithKeywordFramework.TestCase3_Register(TestCase_Register.java:21)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)

is there anybody have an idea to how to solve this thing.

Upvotes: 0

Views: 44

Answers (1)

Stephen C
Stephen C

Reputation: 718788

Assuming1 that TestData is a class and sheet is a static field, then the cause of the NPE is that TestData.sheet is null.

Solution: make sure that TestData.sheet is initialized before you try to call a method on it.

It is unclear why this test works in some contexts and not in others, but my guess is that this test is relying on some other test to initialize TestData.sheets, and hence that the it matters which order the tests are run in. That would be a design flaw in the testcases ...


1 - This assumption is based on the capitalization of the TestData identifier. The standard Java naming conventions stipulate that class names must start with an upper-case letter, and variable names must start with a lower-case letter. (I notice that you are not following this convention in other places ...)

Upvotes: 1

Related Questions