我在从Excel表格中获取数据时遇到了空指针异常。

huangapple go评论97阅读模式
英文:

I am getting Null pointer Exception while fetching data from the excel sheet

问题

  1. public class ContactUsFormTest {
  2. WebDriver driver;
  3. @BeforeMethod
  4. public void run() {
  5. System.setProperty("webdriver.chrome.driver", "Driver Path");
  6. driver = new ChromeDriver();
  7. driver.get("URL");
  8. driver.manage().deleteAllCookies();
  9. driver.manage().window().maximize();
  10. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  11. }
  12. @Test(dataProvider = "contactUs")
  13. public void contactUs(String firstname, String preference, String lastname, String email, String text, String phone) throws InterruptedException {
  14. driver.findElement(By.id("first_name")).sendKeys(firstname);
  15. // 下拉菜单处理
  16. Select sel = new Select(driver.findElement(By.id("preference")));
  17. sel.selectByVisibleText(preference);
  18. driver.findElement(By.id("last_name")).sendKeys(lastname);
  19. driver.findElement(By.id("email")).sendKeys(email);
  20. driver.findElement(By.id("textarea")).sendKeys(text);
  21. driver.findElement(By.id("phone")).sendKeys(phone);
  22. Thread.sleep(2000);
  23. driver.findElement(By.name("submit")).click();
  24. Thread.sleep(3000);
  25. // 验证
  26. Assert.assertTrue(driver.getTitle().contains("Successful submission"), "Page title verified-Submission UnSuccessful");
  27. System.out.println("Page title verified-Successful submission");
  28. }
  29. @DataProvider(name = "contactUs")
  30. public String[][] formData() throws Exception {
  31. Thread.sleep(3000);
  32. ExcelDataConfig eData = new ExcelDataConfig("Excel sheet path");
  33. int rows = eData.getRowCount(0);
  34. String[][] data1 = new String[rows][6];
  35. for (int i = 0; i < rows; i++) {
  36. data1[i][0] = eData.getData(0, i, 0); // 姓氏
  37. data1[i][1] = eData.getData(0, i, 1); // 偏好
  38. data1[i][2] = eData.getData(0, i, 2); // 名字
  39. data1[i][3] = eData.getData(0, i, 3); // 电子邮件
  40. data1[i][4] = eData.getData(0, i, 4); // 文本
  41. data1[i][5] = eData.getData(0, i, 5); // 电话
  42. }
  43. return data1;
  44. }
  45. }

错误信息:

[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 类:

  1. public class ExcelDataConfig {
  2. XSSFWorkbook wb;
  3. XSSFSheet sheet1;
  4. // 构造函数
  5. public ExcelDataConfig(String excelpath) {
  6. try {
  7. File file = new File(excelpath);
  8. FileInputStream fis = new FileInputStream(file);
  9. wb = new XSSFWorkbook(fis);
  10. } catch (Exception e) {
  11. System.out.println(e.getMessage());
  12. }
  13. }
  14. public String getData(int sheetNumber, int row, int column) {
  15. sheet1 = wb.getSheetAt(sheetNumber);
  16. String data = sheet1.getRow(row).getCell(column).getStringCellValue();
  17. return data;
  18. }
  19. public int getRowCount(int sheetIndex) {
  20. int row = wb.getSheetAt(sheetIndex).getLastRowNum();
  21. row = row + 1;
  22. return row;
  23. }
  24. }

我不知道为什么会出现这个错误。

英文:

Code:

  1. public class ContactUsFormTest {
  2. WebDriver driver;
  3. @BeforeMethod
  4. public void run()
  5. {
  6. System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;Driver Path&quot;);
  7. driver=new ChromeDriver();
  8. driver.get(&quot;URL&quot;);
  9. driver.manage().deleteAllCookies();
  10. driver.manage().window().maximize();
  11. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  12. }
  13. @Test(dataProvider=&quot;contactUs&quot;)
  14. public void contactUs(String firstname,String preference,String lastname,String email,String text,String phone) throws InterruptedException
  15. {
  16. driver.findElement(By.id(&quot;first_name&quot;)).sendKeys(firstname);
  17. //drop down handling
  18. Select sel = new Select(driver.findElement(By.id(&quot;preference&quot;)));
  19. sel.selectByVisibleText(preference);
  20. driver.findElement(By.id(&quot;last_name&quot;)).sendKeys(lastname);
  21. driver.findElement(By.id(&quot;email&quot;)).sendKeys(email);
  22. driver.findElement(By.id(&quot;textarea&quot;)).sendKeys(text);
  23. driver.findElement(By.id(&quot;phone&quot;)).sendKeys(phone);
  24. Thread.sleep(2000);
  25. driver.findElement(By.name(&quot;submit&quot;)).click();
  26. Thread.sleep(3000);
  27. //Validation
  28. Assert.assertTrue(driver.getTitle().contains(&quot;Successful submission&quot;), &quot;Page title verified-Submission UnSuccessful&quot;);
  29. System.out.println(&quot;Page title verified-Successful submission&quot;);
  30. }
  31. @DataProvider(name=&quot;contactUs&quot;)
  32. public String[][] formData() throws Exception
  33. {
  34. Thread.sleep(3000);
  35. ExcelDataConfig eData=new ExcelDataConfig(&quot;Excel sheet path&quot;);
  36. int rows=eData.getRowCount(0);
  37. String[][] data1=new String[rows][6];
  38. for(int i=0;i&lt;rows;i++)
  39. {
  40. data1[i][0]=eData.getData(0, i, 0);//first name
  41. data1[i][1]=eData.getData(0, i, 1);//preference
  42. data1[i][2]=eData.getData(0, i, 2);//last name
  43. data1[i][3]=eData.getData(0, i, 3);//email
  44. data1[i][4]=eData.getData(0, i, 4);//text
  45. data1[i][5]=eData.getData(0, i, 5);//phone
  46. }
  47. return data1;
  48. }

}

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:

  1. public class ExcelDataConfig {
  2. XSSFWorkbook wb;
  3. XSSFSheet sheet1;
  4. //Constructor
  5. public ExcelDataConfig(String excelpath) {
  6. try {
  7. File file=new File(excelpath);
  8. FileInputStream fis=new FileInputStream(file);
  9. wb=new XSSFWorkbook(fis);
  10. }
  11. catch (Exception e) {
  12. System.out.println(e.getMessage());
  13. }
  14. }
  15. public String getData(int sheetNumber,int row,int column) {
  16. sheet1=wb.getSheetAt(sheetNumber);
  17. String data=sheet1.getRow(row).getCell(column).getStringCellValue();
  18. return data;
  19. }
  20. public int getRowCount(int sheetIndex)
  21. {
  22. int row=wb.getSheetAt(sheetIndex).getLastRowNum();
  23. row=row+1;
  24. return row;
  25. }

}

I have no clue why I am getting this.

答案1

得分: 0

有许多可能性。所请求的工作表可能不存在。或者行也可能不存在。甚至单元格也可能不存在。你应该添加空值检查。

  1. public String getData(int sheetNumber, int row, int column) {
  2. sheet1 = wb.getSheetAt(sheetNumber);
  3. if (sheet1 == null) {
  4. throw new IllegalArgumentException("工作表编号 " + sheetNumber + " 不存在。");
  5. }
  6. if (sheet1.getRow(row) == null) {
  7. throw new IllegalArgumentException("工作表 " + sheetNumber + " 的行 " + row + " 不存在。");
  8. }
  9. if (sheet1.getRow(row).getCell(column) == null) {
  10. throw new IllegalArgumentException("工作表 " + sheetNumber + " 的行 " + row + " 和列 " + column + " 的单元格不存在。");
  11. }
  12. String data = sheet1.getRow(row).getCell(column).getStringCellValue();
  13. return data;
  14. }

尝试了解一下空指针异常及其修复方法

英文:

There are many possiblities. The requested sheet may not exist. Or the row. Or the cell. You should add null checks.

  1. public String getData(int sheetNumber,int row,int column) {
  2. sheet1=wb.getSheetAt(sheetNumber);
  3. if (sheet1 == null) {
  4. throw new IllegalArgumentException(&quot;Sheet number &quot; + sheetNumber + &quot; does not exist.&quot;
  5. }
  6. if (sheet1.getRow(row) == null) {
  7. throw new IllegalArgumentException(&quot;Row &quot; + row + &quot; in sheet &quot; + sheetNumber + &quot; does not exist.&quot;
  8. }
  9. if (sheet1.getRow(row).getCell(column) == null) {
  10. throw new IllegalArgumentException(&quot;Cell &quot; + column + &quot; in sheet &quot; + sheetNumber + &quot; and row &quot; + row + &quot; does not exist.&quot;
  11. }
  12. String data=sheet1.getRow(row).getCell(column).getStringCellValue();
  13. return data;
  14. }

Try to learn about NullpointerExceptions and how to fix them.

huangapple
  • 本文由 发表于 2020年9月7日 19:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63776731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定