从 excel sheet 获取数据时出现空指针异常

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);
    //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;
 }

}

我在执行测试时从 excel sheet 获取数据,但出现以下错误。

错误:

[实用程序][错误][错误]java.lang.NullPointerException 在 dataDrivenFramework.dataDrivenFramework.ExcelDataConfig.getData(ExcelDataConfig.java:31) 在 dataDrivenFramework.dataDrivenFramework.ContactUsFormTest.formData(ContactUsFormTest.java:63) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)

这里是 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;
    
}

}

我不知道为什么会收到这个。

有很多可能性。请求的 sheet 可能不存在。或行。或者细胞。您应该添加空检查。

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;
}

尝试了解 NullpointerExceptions and how to fix them