英文:
How to read an Excel file in java with DataProvider
问题
我有问题阅读Excel文件中的信息,以便与Selenium一起使用进行测试。但结果总是出现NullPointerException异常。我尝试更改Excel文件中的链接。以下是这段代码的两个部分。
public class TestProviderII {
public static void main(String[] args) {
String url = ".\\src\\test\\resources\\dataProvider.xlsx";
testData(url, "SheetOne");
}
public static void testData(String url, String sheet) {
ExcelUtils excelUtils = new ExcelUtils(url, sheet);
int rowCount = excelUtils.getRowCount();
int colCount = excelUtils.getColCount();
for (int i = 1; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
String cellData = excelUtils.getCellDataString(i, j);
System.out.println("Data : " + cellData);
}
}
}
}
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
static XSSFWorkbook workbook;
static XSSFSheet sheet;
public ExcelUtils(String workBook, String sheetName) {
try {
workbook = new XSSFWorkbook(workBook);
sheet = workbook.getSheet(sheetName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getRowCount() {
int rowCount = 0;
try {
rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("No of Row : " + rowCount);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return rowCount;
}
public static int getColCount() {
int colCount = 0;
try {
colCount = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println("No of Column" + colCount);
} catch (Exception e) {
System.out.println(e.getMessage());
System.println(e.getCause());
e.printStackTrace();
}
return colCount;
}
public static String getCellDataString(int rowNum, int colNum) {
String cellData = "";
try {
cellData = sheet.getRow(rowNum).getCell(colNum).getStringCellValue();
System.out.println("Data Cell : " + cellData);
} catch (Exception e) {
System.out.println(e.getMessage());
System.println(e.getCause());
e.printStackTrace();
}
return cellData;
}
public static Double getCellDataNumber(int rowNum, int colNum) {
double cellData = 0;
try {
cellData = sheet.getRow(rowNum).getCell(colNum).getNumericCellValue();
System.out.println(cellData);
} catch (Exception e) {
System.println(e.getMessage());
System.println(e.getCause());
e.printStackTrace();
}
return cellData;
}
}
结果是NullPointerException异常
null
null
null
null
java.lang.NullPointerException
at test.ExcelUtils.getRowCount(ExcelUtils.java:23)
at test.TestProviderII.testData(TestProviderII.java:12)
at test.TestProviderII.main(TestProviderII.java:7)
java.lang.NullPointerException
at test.ExcelUtils.getColCount(ExcelUtils.java:36)
at test.TestProviderII.testData(TestProviderII.java:13)
at test.TestProviderII.main(TestProviderII.java:7)
Process finished with exit code 0
为什么尽管我按照要求做了,结果却是NullPointerException异常?我还更改了Excel文件链接,结果仍然相同。
String url = ".\\src\\test\\resources\\dataProvider.xlsx";
String url = "src\\test\\resources\\dataProvider.xlsx";
String url = "C:\\Selenuim\\demoApplication\\src\\test\\resources\\dataProvider.xlsx";
英文:
I have trouble reading the information in an Excel file to introduce and test with selenium. But the result always NullPoInterException. I tried to change the link in the Excel file. Here’s the 2-part code.
public class TestProviderII {
public static void main(String[] args) {
String url = ".\\src\\test\\resources\\dataProvider.xlsx";
testData(url, "SheetOne");
}
public static void testData(String url, String sheet) {
ExcelUtils excelUtils = new ExcelUtils(url, sheet);
int rowCount = excelUtils.getRowCount();
int colCount = excelUtils.getColCount();
for (int i = 1; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
String cellData = excelUtils.getCellDataString(i, j);
System.out.println("Data : " + cellData);
}
}
}
}
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
static XSSFWorkbook workbook;
static XSSFSheet sheet;
public ExcelUtils(String workBook, String sheetName) {
try {
workbook = new XSSFWorkbook(workBook);
sheet = workbook.getSheet(sheetName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getRowCount() {
int rowCount = 0;
try {
rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("No of Row : " + rowCount);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return rowCount;
}
public static int getColCount() {
int colCount = 0;
try {
colCount = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println("No of Column" + colCount);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return colCount;
}
public static String getCellDataString(int rowNum, int colNum) {
String cellData = "";
try {
cellData = sheet.getRow(rowNum).getCell(colNum).getStringCellValue();
System.out.println("Data Cell : " + cellData);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return cellData;
}
public static Double getCellDataNumber(int rowNum, int colNum) {
double cellData = 0;
try {
cellData = sheet.getRow(rowNum).getCell(colNum).getNumericCellValue();
System.out.println(cellData);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause());
e.printStackTrace();
}
return cellData;
}
}
And the result is NullPointerException
null
null
null
null
java.lang.NullPointerException
at test.ExcelUtils.getRowCount(ExcelUtils.java:23)
at test.TestProviderII.testData(TestProviderII.java:12)
at test.TestProviderII.main(TestProviderII.java:7)
java.lang.NullPointerException
at test.ExcelUtils.getColCount(ExcelUtils.java:36)
at test.TestProviderII.testData(TestProviderII.java:13)
at test.TestProviderII.main(TestProviderII.java:7)
Process finished with exit code 0
Why the result nullPointerException despite I did exactly what said?
And I changed the Excel file link and the result is the same.
String url = ".\\src\\test\\resources\\dataProvider.xlsx";
String url = "src\\test\\resources\\dataProvider.xlsx";
String url = "C:\\Selenuim\\demoApplication\\src\\test\\resources\\dataProvider.xlsx
答案1
得分: 1
将以下行更改为:
testData(url, "Sheet1");
假设您的Excel文件中的表格如下所示:
还要确保您的Excel表中有一些数据。我只需将其更改为Sheet1
并运行了您的代码。如下所示:
Excel表格:
控制台输出:
列3的数量
数据单元格:d
数据:d
数据单元格:e
数据:e
数据单元格:f
数据:f
英文:
Change below line:
testData(url, "SheetOne");
to:
testData(url, "Sheet1");
Assuming the sheet looks like below in your excel file:
Also ensure, there is some data in your excel sheet. I ran your code by just changing to Sheet1
. And it worked see below:
Excel sheet:
Console Output:
No of Column3
Data Cell : d
Data : d
Data Cell : e
Data : e
Data Cell : f
Data : f
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论