0

I am reading an excel file using Apache POI API. I have written a Utility class. But when i call this Method, I got an error which is mentioned below. Please help me to find out the error.

public class TestUtil {

    public static int PAGE_LOAD_TIME = 20;
    public static int IMPLICIT_WAIT = 10;
    static Workbook book;`enter code here`
    static Sheet sheet;

    public static Object[][] getTestData(String sheetName){
        FileInputStream file = null;
        try {
                file = new FileInputStream("/home/khawer/eclipse-workspace/DemoSite/src/main/java/com/qa/demo/testdata/testdata.xlsx");
                book = WorkbookFactory.create(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sheet = book.getSheet(sheetName);
        Object[][] data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];

        for(int i = 0; i < sheet.getLastRowNum(); i++) {

            for(int j = 0; j < sheet.getRow(0).getLastCellNum(); j++) {

                data[i][j] = sheet.getRow(i + 1).getCell(j).toString();
            }   
        }

        return data;
   }

}
[Utils] [ERROR] [Error] java.lang.NullPointerException
    at com.qa.demo.util.TestUtil.getTestData(TestUtil.java:38)
    at com.qa.demo.testcases.ContactsTest.getDemoData(ContactsTest.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
khawar
  • 112
  • 1
  • 5

1 Answers1

0

As inferno mentioned check line number 38 as we cannot guess the line 38 in the mentioned code.

Possible reasons are, 1.Wrong path or excel tab(incase multiple tabs exist in single excel) 2.If the sheet is completly empty 3.If there is any blank line at the top or inbetween the rows.

Sadha Nanda
  • 337
  • 4
  • 15
  • Here is line no. 38 ==> data[i][j] = sheet.getRow(i + 1).getCell(j).toString(); – khawar Oct 24 '19 at 07:38
  • looks like some issue with the cell from which this line of code is trying to fetch the data..please check whether the variables i and j is actually pointing to the cell which you are trying to get the data.Most probably the cell is empty or you are pointing wrong sheet (incase u have multiple tabs in same excel sheet) – Sadha Nanda Oct 24 '19 at 07:47