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

Satish Rongala

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.

Stephen C

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

java.util.ArrayList cannot be cast to org.testng.xml.XmlClass - This error is thrown while running the script

Getting Transformer exception while running Jmeter script

while loop not working in shell script while running the script from crontab

Getting an error when running my script in a rule

Getting an error when running awk from a script

Getting a not subscriptable when running a web scraping script

Why getting invalid literal when running this script?

Getting 'missing or invalid option' when running this script

Running python script from matlab not working, but runs fine from terminal

Nohup for Python script not working when running in the background with &

Why working script fails when running in the background?

Getting cucumber.runtime.CucumberException: Couldn't load plugin class: prettty while running cucmber script using Testng

List comprehensions work fine in Python shell but are ignored when running the script

When running bash script that calls another script getting EOF error

Getting exception while running cucumber with junit / Testng

Error when running a script

RuntimeError when running script?

Getting error while running the node.js server side script

Getting user input while running a python script in atom

Getting TypeError: Unknown encoding: 1 while running the server script with Mongoose

While running sed command through chef script getting error

Getting syntax error while running a SQL script in psql

iOS - Application crashes only when running directly on simulator but working fine while debugging or running through Xcode

i am getting a java.lang.NullPointerException while executing the testng script

Setting a polling-script while ajax is running not working

Is it possible to continue working in Revit while running a python script in pyrevit?

While running suits using testng.xml getting null pointer Exception , While able to Run Individual tests case WITH TESTNG

AHK script is unstable while running

Python KeyError while running script

TOP Ranking

HotTag

Archive