title: TestNG Exception : Cannot Instantiate class
body: <p>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</p>
  
  <p>Here is my test base class</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>Here is my Test class where i want to initialize web elements and also write the scripts</p>
  
  <pre><code>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!");
  
  
  
  
      }
  </code></pre>
  
  <p>But i am getting java null pointer exception. Here is the log</p>
  
  <pre><code>  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.&lt;init&gt;(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.&lt;init&gt;(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.&lt;init&gt;(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.&lt;init&gt;(FirefoxDriver.java:108)
      at org.openqa.selenium.firefox.FirefoxDriver.&lt;init&gt;(FirefoxDriver.java:104)
      at com.test.TVO_Assignment.Testing_TVO_Website.&lt;init&gt;(Testing_TVO_Website.java:34)
      ... 30 more
  </code></pre>