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.
Advertisement
Answer
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.