0

I am new to Keyword Driven approach for selenium. I am getting NullPointerException while running below ExecuteTest.java

Folder Structure

  1. Folder structure

Object.txt

  1. Object.txt

TestCase.xlsx

  1. TestCase.xlsx

Error Screenshot

  1. Screenshot1
  2. Screenshot2

    Adding Debug screenshots screenshot1 screenshot2 screenshot3

ReadGuru99Excel.java

import org.apache.poi.ss.usermodel.Sheet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ReadGuru99ExcelFile {

    public Sheet readExcel (String filePath,String fileName, String sheetName)throws IOException{
        File file = new File(filePath+"\\"+fileName);
        FileInputStream inputStream = new FileInputStream(file);
        Workbook guru99Workbook = null;

        String fileExtensionName = fileName.substring(fileName.indexOf("."));

        if(fileExtensionName.equals(".xlsx")){
            guru99Workbook = new XSSFWorkbook(inputStream);
        } else if(fileExtensionName.equals(".xls")) {
            guru99Workbook = new HSSFWorkbook(inputStream);
        }
        Sheet guru99Sheet =guru99Workbook.getSheet(sheetName);
        return guru99Sheet;
    }
}

ReadObject.Java

package operation;
import java.util.Properties;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;

public class ReadObject {   

    Properties p = new Properties();

    public Properties getObjectRepository()throws IOException{

        InputStream stream = new FileInputStream(new File (System.getProperty("user.dir")+"\\src\\objects\\object.txt"));

        p.load(stream);
        return p;
    }
}

UIOperation.java

package operation;
import org.openqa.selenium.WebDriver;
import java.util.Properties;
import org.openqa.selenium.By;

public class UIOperation {
    WebDriver driver ;

    public UIOperation(WebDriver driver){
        this.driver = driver; 
    }

    public void perform(Properties p, String operation, String objectName, String objectType, String value) throws Exception{
        System.out.println("");
        switch(operation.toUpperCase()){
            case "CLICK":
                driver.findElement(this.getObject(p, objectName, objectType)).click();
                break;
            case "SETTEXT":
                driver.findElement(this.getObject(p, objectName, objectType)).sendKeys(value);
                break;
            case "GOTOURL":
                driver.get(p.getProperty(value));
                break;
            case "GETTEXT":
                driver.findElement(this.getObject(p, objectName, objectType)).getText();
                break;
            default:
                break;
       }
   }
   private By getObject(Properties p, String objectName, String objectType) throws Exception{
      if(objectType.equalsIgnoreCase("XPATH")){
          return By.xpath(p.getProperty(objectName));
      }else if(objectType.equalsIgnoreCase("CLASSNAME")){
          return By.className(p.getProperty(objectName));
      }else if(objectType.equalsIgnoreCase("NAME")){
          return By.name(p.getProperty(objectName));
      }else if(objectType.equalsIgnoreCase("CSS")){
          return By.cssSelector(p.getProperty(objectName));
      }else if(objectType.equalsIgnoreCase("LINK")){
          return By.linkText(p.getProperty(objectName));
      }else if(objectType.equalsIgnoreCase("PARTIALLINK")){
          return By.partialLinkText(p.getProperty(objectName));
      }else{
          throw new Exception("Wrong object type");
      }
   }
}

ExecuteTest.java

package testcases;
import java.util.Properties;
import operation.ReadObject;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import operation.UIOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import excelExportAndFileIO.ReadGuru99ExcelFile;

public class ExecuteTest {

    @Test
    public void testLogin()throws Exception{
        /**
         * System.setProperty("webdriver.gecko.driver", "E:\\Selenium-2017\\geckodriver-v0.18.0-win64\\geckodriver.exe");
         * //Now you can Initialize marionette driver to launch firefox
         * DesiredCapabilities capabilities = DesiredCapabilities.firefox();
         * capabilities.setCapability("marionette", true);
         * WebDriver driver = new RemoteWebdriver(capabilities); 
         *
         * WebDriver webdriver = new FirefoxDriver();
         **/
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver","E:\\Selenium-2017\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();

        ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();
        ReadObject object = new ReadObject();
        Properties allObjects = object.getObjectRepository();
        UIOperation operation = new UIOperation(driver);
        Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\test-output","TestCase.xlsx","KeywordFramework");
        int rowCount =guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();

        System.out.println("first step clear");

        for(int i = 0;i<rowCount+1; i++){
            Row row  =guru99Sheet.getRow(i);
            System.out.println("2nd clear");
            if(row.getCell(0).toString().length()==0){
                System.out.println("4nd clear");
                System.out.println(row.getCell(1).toString()+"-----"+row.getCell(2).toString()+"----"+row.getCell(3).toString()+"---"+row.getCell(4).toString());
                System.out.println("3rd clear");
                operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());  

            } else
                System.out.println("New Testcase->" + row.getCell(0).toString()+"Started");
            System.out.println("testerassumption");

        }
    }
}

1 Answers1

0

First of all, please see this wonderful post about NullPointerException and how to fix it.

In your case, the stacktrace points the NullPointerException to be occuring at line 49 of the code in ExecuteTest.java, which points to this line of code

 operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());

The issue here is that when you're trying to convert the contents of the cell in the getCell() method to a String, if there is nothing in the cell, then there would always be a NullPointerException.

You can either place a != null check before every cell or add a " " for every toString() method, like

  operation.perform(allObjects, (row.getCell(1)+"").toString(), (row.getCell(2)+"").toString(), (row.getCell(3)+"").toString(), (row.getCell(4)+"").toString());
demouser123
  • 4,108
  • 9
  • 50
  • 82
  • Thanks!! @demouser123 by adding " " for every toString( ) I am not getting any null pointer exception now.It works!! Thanks for sharing the Post link ,It helped me a lot in understanding null pointer exception. – karishma yadav Jul 19 '17 at 17:23