Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver 为什么我得到以下代码的NullPointerException_Selenium Webdriver_Testng Dataprovider - Fatal编程技术网

Selenium webdriver 为什么我得到以下代码的NullPointerException

Selenium webdriver 为什么我得到以下代码的NullPointerException,selenium-webdriver,testng-dataprovider,Selenium Webdriver,Testng Dataprovider,我正在使用ApachePOIAPI读取excel文件。我写了一个实用类。但是当我调用这个方法时,我得到了一个错误,如下所述。请帮我找出错误 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;

我正在使用ApachePOIAPI读取excel文件。我写了一个实用类。但是当我调用这个方法时,我得到了一个错误,如下所述。请帮我找出错误

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)
公共类TestUtil{
公共静态整型页面加载时间=20;
公共静态int隐式_WAIT=10;
静态工作簿;`在此处输入代码`
静态薄板;
公共静态对象[][]getTestData(字符串名称){
FileInputStream文件=null;
试一试{
file=newfileinputstream(“/home/khawer/eclipseworkspace/DemoSite/src/main/java/com/qa/demo/testdata/testdata.xlsx”);
book=WorkbookFactory.create(文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(无效格式){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
sheet=book.getSheet(sheetName);
对象[][]数据=新对象[sheet.getLastRowNum()][sheet.getRow(0.getLastCellNum()];
对于(int i=0;i
如《地狱》所述,检查第38行,因为我们无法猜出所述代码中的第38行

可能的原因是,, 1.错误路径或excel选项卡(如果单个excel中存在多个选项卡) 2.如果工作表完全为空
3.如果在行的顶部或行之间有任何空行。

我们无法猜测第38行是哪一个代码,这里是第38行==>data[i][j]=sheet.getRow(i+1).getCell(j).toString();这行代码试图从中获取数据的单元格似乎有问题。请检查变量i和j是否实际指向您试图获取数据的单元格。很可能单元格为空,或者您指向了错误的工作表(如果您在同一excel工作表中有多个选项卡)