0

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.

3
  • Which line is Functionlibrary.java:50 ? Commented Aug 13, 2018 at 11:00
  • for (int Tests = 1; Tests < TestData.sheet.getLastRowNum() + 1; Tests++) Commented Aug 14, 2018 at 7:43
  • hi Stephen, i found that Excel row count is throwing NullPointErrorException,, but i am not getting why it is not throwing null point error when running with "Running Testng Programatically" Commented Aug 14, 2018 at 8:02

1 Answer 1

1

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 ...)

Sign up to request clarification or add additional context in comments.

7 Comments

but why it is not throwing when running Testng programtically
i added my Excel class that reads the Excel data.. can you please check that once
1) TestData.sheet and Excel.sheet are not the same thing. 2) Did you check the test output for a stacktrace printed by loadExcelSheet ? 3) I don't see where you call loadExcelSheet.
tht logic what you asked i understood clearly but i didnt callthe loadExcelSheet function
and i tried to print the value in the point of debugging and i didn't get the data..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.