title: Cannot Instantiate class error - Selenium WebDriver
body: <p>I am facing this 'Cannot insantiate class' error on running one of my test cases in selenium webdriver using java.</p>
  
  <p>Below is the class of the functionality of the test,</p>
  
  <pre><code>package Pages;
  
  import java.util.List;
  
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.Select;
  import org.openqa.selenium.support.ui.WebDriverWait;
  
  import Lib.lib;
  
  public class KnowledgeBase extends lib{
  
      By sortBylink = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/div");
      By featuredOption = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]");
      By mostRcnt = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[2]");
  
      String featOption = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]")).getText();
  
      String mostRecent = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[2]")).getText();
  
      public void initSBy() throws Exception
      {
          driver.findElement(sortBylink).click();
          Thread.sleep(1500);
      }
  
      public void selectfO() throws Exception
      {
          driver.findElement(featuredOption).click();
          Thread.sleep(5000);
      }
  
      public void selectMr() throws Exception
      {
          driver.findElement(mostRcnt).click();
          Thread.sleep(5000);
      }
  
      public void sortBy(String sProp) throws Exception
      {
          this.initSBy();
  
          if (sProp == featOption) {
  
              this.selectfO();
  
          }
  
          else if (sProp == mostRecent){
  
              this.selectMr();
          }
  
          else {
  
              System.out.println("Incorrect option. Test failed.");
          }
      }
  
  
  }
  </code></pre>
  
  <p>Below is my Test Case Class</p>
  
  <pre><code>package TestCases;
  
  import org.testng.annotations.Test;
  
  import Lib.lib;
  import Pages.KnowledgeBase;
  import Pages.LoginPage;
  
  public class sortingTextKB extends lib {
  
      LoginPage uLogin = new LoginPage();
      KnowledgeBase sortObj = new KnowledgeBase();
  
      //Logging In
      @Test (priority = 1)
      public void loggingIn() throws Exception
      {
          uLogin.loginToKB("uzii@test.com", "uziiiii");
      }
  
      //Sorting
      @Test (priority = 2)
      public void sortIn() throws Exception
      {
          sortObj.sortBy("Most Recent");
      }
  
  }
  </code></pre>
  
  <p>Below is my Lib class, that contains the chrome driver configuration</p>
  
  <pre><code>package Lib;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.chrome.ChromeOptions;
  import org.testng.annotations.BeforeTest;
  import org.testng.annotations.AfterTest;
  
  public class lib {
  
      protected static WebDriver driver = null;
  
      @BeforeTest
      public void chrome_extension()
      {
          System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
          ChromeOptions options = new ChromeOptions();
          options.addArguments("--start-maximized");
          driver = new ChromeDriver(options);
          driver.get("http://www.testsite.com");
      }
  
  
      @AfterTest
      public void quit()
      {
          driver.quit();
      }
  
  }
  </code></pre>
  
  <p>When I run my test case class, I am getting the following error,</p>
  
  <pre><code>org.testng.TestNGException: 
  Cannot instantiate class TestCases.sortingTextKB
      at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
      at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
      at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
      at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
      at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
      at org.testng.internal.TestNGClassFinder.&lt;init&gt;(TestNGClassFinder.java:120)
      at org.testng.TestRunner.initMethods(TestRunner.java:409)
      at org.testng.TestRunner.init(TestRunner.java:235)
      at org.testng.TestRunner.init(TestRunner.java:205)
      at org.testng.TestRunner.&lt;init&gt;(TestRunner.java:160)
      at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
      at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
      at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
      at org.testng.SuiteRunner.init(SuiteRunner.java:157)
      at org.testng.SuiteRunner.&lt;init&gt;(SuiteRunner.java:111)
      at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
      at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
      at org.testng.TestNG.run(TestNG.java:1057)
      at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
      ... 21 more
  Caused by: java.lang.NullPointerException
      at Pages.KnowledgeBase.&lt;init&gt;(KnowledgeBase.java:22)
      at TestCases.sortingTextKB.&lt;init&gt;(sortingTextKB.java:12)
      ... 26 more
  </code></pre>
  
  <p>Following is the 22 line of KnowledgeBase class,</p>
  
  <pre><code>String featOption = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]")).getText();
  </code></pre>
  
  <p>Please let me know why I am facing this 'cannot instantiate class' error. Thanks</p>