0

I'm stuck on a Null Pointer Exception problem. The tests are run through JUnit, but I didn't manage to debug properly and find out the reason of it.

Test class

ExcelActions excelActions;
Excel_Import excel_import;

@Test
public void addResultsToExcel() throws InterruptedException {
    searchAfterLogin(); // works fine until the next method
    excelActions.addToExcel();  //thats where NPE begins...
    excel_import.importExcel(excelActions.addToExcel());

}

Excel Action (Webdriver part)

public class ExcelActions extends BaseAction {

public ExcelActions(WebDriver driver) { super(driver); }

public Map<String, Object[]> addToExcel(){

    String beforeXpath_pageName = "//*[@id=\"mw-content- 
text\"]/div[3]/ul/li[";
    String afterXpath_pageName = "]/div[1]/a";

    int rowCount ;
    rowCount = 20;
    Map<String, Object[]> data = new TreeMap<String, Object[]>();

    for(int i = 1; i <= rowCount; i++){

        String actualXpath_pageName = beforeXpath_pageName + i + 
afterXpath_pageName;
        String pageName = 
driver.findElement(By.xpath(actualXpath_pageName)).getText();
        System.out.println(pageName);

        data.put("" + i, new Object[]{pageName});
    }
    return data;
}
}

Excel import class

public class Excel_Import extends BaseAction {

    public Excel_Import(WebDriver driver) { super(driver); }

public void importExcel(Map<String, Object[]> data) {
    XSSFWorkbook workbook = new XSSFWorkbook();

    //Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Data");

    //Iterate over data and write to sheet
    Set<String> keyset = data.keySet();

    int rownum = 0;
    for (String key : keyset)
    {
        //create a row of excel sheet
        Row row = sheet.createRow(rownum++);

        //get object array of particular key
        Object[] objArr = data.get(key);

        int cellnum = 0;

        for (Object obj : objArr)
        {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String)
            {
                cell.setCellValue((String) obj);
            }
            else if (obj instanceof Integer)
            {
                cell.setCellValue((Integer) obj);
            }
        }
    }
    try
    {
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new 
File("D:\\Projects\\Cucumber\\src\\main\\resources\\ImportedResults.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("howtodoinjava_demo.xlsx written successfully on 
disk.");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
}

Could you guys have a look at it?

Guy
  • 46,488
  • 10
  • 44
  • 88

1 Answers1

1

The field:

ExcelActions excelActions;

is not initialized. You should assign an instance of ExcelActions to it, e.g.:

ExcelActions excelActions = new ExcelActions(someWebDriverMock);

It is not enough to declare a field in a class. You need some specific object to be assigned to it, otherwise it will be null.

Tosz
  • 308
  • 1
  • 5
  • 14