0

I am getting a java null pointer exception. I am not able to solve it. I have initialize driver in my testbase class and want to use the same driver in my Testing_TVO class

Here is my test base class

public class testbase {

public static WebDriver driver;
String baseURL = "https://mpower.tvo.org/educators/#/login/";
String browser  = "firefox";


public void init()
{
    selectBrowser(browser);
    getUrl(baseURL);
}

public void selectBrowser(String browser)
{
    if(browser.equalsIgnoreCase("firefox"))
    {
        System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe ");
        driver = new FirefoxDriver();
    }
    if (browser.equalsIgnoreCase("chrome"))
    {
        System.setProperty("webdriver.chrome.driver", "H:\\geckodriver-v0.17.0-win64\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    if(browser.equalsIgnoreCase("edge"))
    {
         System.setProperty("webdriver.edge.driver", "H:\\MicrosoftWebDriver.exe");
         driver = new EdgeDriver();
    }

}

public void getUrl(String url)
{
    driver.get(url);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

Here is my Test class where i want to initialize web elements and also write the scripts

public class Testing_TVO_Website extends testbase {


    @BeforeClass
    public void beforeClass() {
        init();

    }

    WebElement register_btn = driver.findElement(By.xpath(".//*[@id='loginPageRegister1']"));
    WebElement firstName_tbx = driver.findElement(By.xpath(".//*[@id='firstName']"));   
    WebElement lastName_tbx = driver.findElement(By.xpath(".//*[@id='lastName']"));

    @FindBy(xpath = ".//*[@id='educatorRoleId']")
    WebElement role;
    Select role_sel = new Select(role);

    @FindBy(xpath = ".//*[@id='boardId']")
    WebElement board;
    Select board_sel = new Select(board);

    @FindBy(xpath = ".//*[@id='schoolId']")
    WebElement school;
    Select school_sel = new Select(school);


    WebElement email_tbx = driver.findElement(By.xpath(".//*[@id='email']"));
    WebElement emailConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userEmailConfirm']"));
    WebElement password_tbx = driver.findElement(By.xpath(".//*[@id='userPassword']"));
    WebElement passwordConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userPasswordConfirm']"));

    @FindBy(xpath = ".//*[@id='source']")
    WebElement source;
    Select source_sel = new Select(source);

    WebElement agreement_chbx = driver.findElement(By.xpath(".//*[@id='agreement']"));
    WebElement registerSubmit_btn = driver.findElement(By.xpath(".//*[@id='registerSubmit']"));



    @Test
    public void TC_001_Verifying_Registeration() {
      register_btn.click(); 
      firstName_tbx.sendKeys("Shivam");
      lastName_tbx.sendKeys("Patel");
      role_sel.selectByIndex(1);
      board_sel.selectByValue("Algoma DSB");
      school_sel.selectByVisibleText("Blind River");

      email_tbx.sendKeys("patelshivam3033@adsb.on.ca");
      emailConfirm_tbx.sendKeys("patelshivam3033@adsb.on.ca");
      password_tbx.sendKeys("shivampatel");
      passwordConfirm_tbx.sendKeys("shivampatel");

      source_sel.selectByIndex(2);
      agreement_chbx.click();
      registerSubmit_btn.click();

      assertEquals(driver.findElement(By.xpath(".//*[@id='app-content']/div[3]/div/div/div[2]/h2")), "Thank you for registering!");




    }

But i am getting java null pointer exception. Here is the log

  org.testng.TestNGException: 
Cannot instantiate class com.test.TVO_Assignment.Testing_TVO_Website
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
    at org.testng.TestClass.getInstances(TestClass.java:99)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
    at org.testng.TestClass.init(TestClass.java:77)
    at org.testng.TestClass.<init>(TestClass.java:42)
    at org.testng.TestRunner.initMethods(TestRunner.java:423)
    at org.testng.TestRunner.init(TestRunner.java:250)
    at org.testng.TestRunner.init(TestRunner.java:220)
    at org.testng.TestRunner.<init>(TestRunner.java:169)
    at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
    at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
    at org.testng.SuiteRunner.init(SuiteRunner.java:185)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
    ... 25 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
    at com.test.TVO_Assignment.Testing_TVO_Website.<init>(Testing_TVO_Website.java:34)
    ... 30 more
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Amalius Nov 21 '17 at 09:19
  • @sp4 Where are you initializing the pageobject? Though as suggested below you would be better off separating. – Grasshopper Nov 22 '17 at 06:20

1 Answers1

1

As per your log, the error is not null pointer exception and the actual error is IllegalStateException beacuse the gecko driver is not set. The issue here is, there is extra space at end of the gecko driver path. It may be a typo.

System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe ");

Kindly remove the space at end of the above line. It resolve your issue.

Also, there is problem in your code like, you cann't combine the page factory code and test code in a single class make it separate.

Murthi
  • 5,299
  • 1
  • 10
  • 15