0

I get NullPointerException when I run this code -

File src = new File("C:\\myxl.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh1 = wb.getSheetAt(0);

String mystr=sh1.getRow(1).getCell(4).getStringCellValue();
WebElement mwe = driver.findElement(By.xpath("//div[@title='" + mystr + "']"));

if (mystr.contains(null))
{
System.out.println("there is Nothing");
}
else 
{
act.doubleClick(mwe).perform();

What do I change in the code so that if the value in that column and row is empty it will print "there is Nothing" instead of giving me a NullPointerException or else it will take some action with the element.

P.S. This is part a of a Selenium webdriver script

Ami
  • 61
  • 9
  • if (mystr == null) – Atul Kumbhar Mar 10 '16 at 22:19
  • @AtulKumbhar Still it gives me this error – Ami Mar 10 '16 at 22:22
  • stacktrace please? – Atul Kumbhar Mar 10 '16 at 22:23
  • @AtulKumbhar a student here not sure what do you mean by stacktrace yet, Sorry. If you mean where do i get the NullPointerException I get it on line 157 which is this line of code String mystr=sh1.getRow(1).getCell(4).getStringCellValue(); – Ami Mar 10 '16 at 22:33
  • you answered the question then...check which one of these is null before setting mystr: if(sh1 != null && sh1.getRow(1) != null && sh1.getRow(1).getCell(4) != null) mystr=sh1.getRow(1).getCell(4).getStringCellValue(); – Atul Kumbhar Mar 10 '16 at 22:38
  • @AtulKumbhar Thank you so much it worked. – Ami Mar 10 '16 at 22:48
  • Final code looks like this, if anyone needs it, thanks so much @Atul again`File src = new File("C:\\myxl.xlsx"); FileInputStream fis = new FileInputStream(src); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sh1 = wb.getSheetAt(0); if (sh1 != null && sh1.getRow(1) != null && sh1.getRow(1).getCell(4) != null) { mystr=sh1.getRow(1).getCell(4).getStringCellValue(); WebElement mwe = driver.findElement(By.xpath("//div[@title='" + mystr + "']")); act.doubleClick(mwe).perform(); else { System.out.println("there is Nothing"); } ` – Ami Mar 10 '16 at 22:52

0 Answers0