英文:
I am getting Null pointer Exception while fetching data from the excel sheet
问题
public class ContactUsFormTest {
WebDriver driver;
@BeforeMethod
public void run() {
System.setProperty("webdriver.chrome.driver", "Driver Path");
driver = new ChromeDriver();
driver.get("URL");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider = "contactUs")
public void contactUs(String firstname, String preference, String lastname, String email, String text, String phone) throws InterruptedException {
driver.findElement(By.id("first_name")).sendKeys(firstname);
// 下拉菜单处理
Select sel = new Select(driver.findElement(By.id("preference")));
sel.selectByVisibleText(preference);
driver.findElement(By.id("last_name")).sendKeys(lastname);
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.id("textarea")).sendKeys(text);
driver.findElement(By.id("phone")).sendKeys(phone);
Thread.sleep(2000);
driver.findElement(By.name("submit")).click();
Thread.sleep(3000);
// 验证
Assert.assertTrue(driver.getTitle().contains("Successful submission"), "Page title verified-Submission UnSuccessful");
System.out.println("Page title verified-Successful submission");
}
@DataProvider(name = "contactUs")
public String[][] formData() throws Exception {
Thread.sleep(3000);
ExcelDataConfig eData = new ExcelDataConfig("Excel sheet path");
int rows = eData.getRowCount(0);
String[][] data1 = new String[rows][6];
for (int i = 0; i < rows; i++) {
data1[i][0] = eData.getData(0, i, 0); // 姓氏
data1[i][1] = eData.getData(0, i, 1); // 偏好
data1[i][2] = eData.getData(0, i, 2); // 名字
data1[i][3] = eData.getData(0, i, 3); // 电子邮件
data1[i][4] = eData.getData(0, i, 4); // 文本
data1[i][5] = eData.getData(0, i, 5); // 电话
}
return data1;
}
}
错误信息:
[Utils] [ERROR] [Error] java.lang.NullPointerException
at dataDrivenFramework.dataDrivenFramework.ExcelDataConfig.getData(ExcelDataConfig.java:31)
at dataDrivenFramework.dataDrivenFramework.ContactUsFormTest.formData(ContactUsFormTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source);
这是 ExcelDataConfig 类:
public class ExcelDataConfig {
XSSFWorkbook wb;
XSSFSheet sheet1;
// 构造函数
public ExcelDataConfig(String excelpath) {
try {
File file = new File(excelpath);
FileInputStream fis = new FileInputStream(file);
wb = new XSSFWorkbook(fis);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public String getData(int sheetNumber, int row, int column) {
sheet1 = wb.getSheetAt(sheetNumber);
String data = sheet1.getRow(row).getCell(column).getStringCellValue();
return data;
}
public int getRowCount(int sheetIndex) {
int row = wb.getSheetAt(sheetIndex).getLastRowNum();
row = row + 1;
return row;
}
}
我不知道为什么会出现这个错误。
英文:
Code:
public class ContactUsFormTest {
WebDriver driver;
@BeforeMethod
public void run()
{
System.setProperty("webdriver.chrome.driver", "Driver Path");
driver=new ChromeDriver();
driver.get("URL");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider="contactUs")
public void contactUs(String firstname,String preference,String lastname,String email,String text,String phone) throws InterruptedException
{
driver.findElement(By.id("first_name")).sendKeys(firstname);
//drop down handling
Select sel = new Select(driver.findElement(By.id("preference")));
sel.selectByVisibleText(preference);
driver.findElement(By.id("last_name")).sendKeys(lastname);
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.id("textarea")).sendKeys(text);
driver.findElement(By.id("phone")).sendKeys(phone);
Thread.sleep(2000);
driver.findElement(By.name("submit")).click();
Thread.sleep(3000);
//Validation
Assert.assertTrue(driver.getTitle().contains("Successful submission"), "Page title verified-Submission UnSuccessful");
System.out.println("Page title verified-Successful submission");
}
@DataProvider(name="contactUs")
public String[][] formData() throws Exception
{
Thread.sleep(3000);
ExcelDataConfig eData=new ExcelDataConfig("Excel sheet path");
int rows=eData.getRowCount(0);
String[][] data1=new String[rows][6];
for(int i=0;i<rows;i++)
{
data1[i][0]=eData.getData(0, i, 0);//first name
data1[i][1]=eData.getData(0, i, 1);//preference
data1[i][2]=eData.getData(0, i, 2);//last name
data1[i][3]=eData.getData(0, i, 3);//email
data1[i][4]=eData.getData(0, i, 4);//text
data1[i][5]=eData.getData(0, i, 5);//phone
}
return data1;
}
}
I am fetching the data from the excel sheet while performing test I am getting below error.
Error:
[Utils] [ERROR] [Error] java.lang.NullPointerException
at dataDrivenFramework.dataDrivenFramework.ExcelDataConfig.getData(ExcelDataConfig.java:31)
at dataDrivenFramework.dataDrivenFramework.ContactUsFormTest.formData(ContactUsFormTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
Here is ExcelDataConfig class:
public class ExcelDataConfig {
XSSFWorkbook wb;
XSSFSheet sheet1;
//Constructor
public ExcelDataConfig(String excelpath) {
try {
File file=new File(excelpath);
FileInputStream fis=new FileInputStream(file);
wb=new XSSFWorkbook(fis);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public String getData(int sheetNumber,int row,int column) {
sheet1=wb.getSheetAt(sheetNumber);
String data=sheet1.getRow(row).getCell(column).getStringCellValue();
return data;
}
public int getRowCount(int sheetIndex)
{
int row=wb.getSheetAt(sheetIndex).getLastRowNum();
row=row+1;
return row;
}
}
I have no clue why I am getting this.
答案1
得分: 0
有许多可能性。所请求的工作表可能不存在。或者行也可能不存在。甚至单元格也可能不存在。你应该添加空值检查。
public String getData(int sheetNumber, int row, int column) {
sheet1 = wb.getSheetAt(sheetNumber);
if (sheet1 == null) {
throw new IllegalArgumentException("工作表编号 " + sheetNumber + " 不存在。");
}
if (sheet1.getRow(row) == null) {
throw new IllegalArgumentException("工作表 " + sheetNumber + " 的行 " + row + " 不存在。");
}
if (sheet1.getRow(row).getCell(column) == null) {
throw new IllegalArgumentException("工作表 " + sheetNumber + " 的行 " + row + " 和列 " + column + " 的单元格不存在。");
}
String data = sheet1.getRow(row).getCell(column).getStringCellValue();
return data;
}
尝试了解一下空指针异常及其修复方法。
英文:
There are many possiblities. The requested sheet may not exist. Or the row. Or the cell. You should add null checks.
public String getData(int sheetNumber,int row,int column) {
sheet1=wb.getSheetAt(sheetNumber);
if (sheet1 == null) {
throw new IllegalArgumentException("Sheet number " + sheetNumber + " does not exist."
}
if (sheet1.getRow(row) == null) {
throw new IllegalArgumentException("Row " + row + " in sheet " + sheetNumber + " does not exist."
}
if (sheet1.getRow(row).getCell(column) == null) {
throw new IllegalArgumentException("Cell " + column + " in sheet " + sheetNumber + " and row " + row + " does not exist."
}
String data=sheet1.getRow(row).getCell(column).getStringCellValue();
return data;
}
Try to learn about NullpointerExceptions and how to fix them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论