从excel工作表中获取数据时出现空指针异常

m4pnthwp  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(964)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
4个月前关门了。
代码:

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表中提取数据。我发现以下错误。
错误:
[utils][error][error]java.lang.nullpointerexception位于datadrivenframework.datadrivenframework.exceldataconfig.getdata(exceldataconfig)。java:31)在datadrivenframework.datadrivenframework.contactusformtest.formdata(contactusformtest。java:63)位于sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)sun.reflect.delegatingmethodaccessorimpl.invoke(未知源)上的sun.reflect.nativemethodaccessorimpl.invoke(未知源)
下面是exceldataconfig类:

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;

}

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

isr3a4wc

isr3a4wc1#

有许多可能性。请求的工作表可能不存在。或者那排。或者牢房。您应该添加空检查。

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以及如何修复它们。

相关问题