1

I am facing this 'Cannot insantiate class' error on running one of my test cases in selenium webdriver using java.

Below is the class of the functionality of the test,

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.");
        }
    }


}

Below is my Test Case Class

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("[email protected]", "uziiiii");
    }

    //Sorting
    @Test (priority = 2)
    public void sortIn() throws Exception
    {
        sortObj.sortBy("Most Recent");
    }

}

Below is my Lib class, that contains the chrome driver configuration

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();
    }

}

When I run my test case class, I am getting the following error,

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.<init>(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.<init>(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.<init>(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.<init>(KnowledgeBase.java:22)
    at TestCases.sortingTextKB.<init>(sortingTextKB.java:12)
    ... 26 more

Following is the 22 line of KnowledgeBase class,

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();

Please let me know why I am facing this 'cannot instantiate class' error. Thanks

8
  • You know what line is causing the issue... what have you tried to solve it? Commented Sep 12, 2015 at 0:33
  • @JeffC I am unable to figure out why is this not instantiating the class. If I dont use above line 22 , then it might work, but I need to get the text, so I can compare it with the one I give at run time. Commented Sep 12, 2015 at 12:03
  • Caused by: java.lang.NullPointerException have you tried googling what a NullPointerException is? Then look at line 22 and do some troubleshooting. Post the code that you tried and what didn't work. Commented Sep 12, 2015 at 13:25
  • @JeffC the case here is that, in my KnowledgeBase class in the function sortBy(String sProp), if I give hard coded values for "sProp" inside 'if statements' then my case is working fine, but if i pass the string in them using driver.find....getText(); statement then I am getting cannot instantiate class error. I dont know what is the issue with that if I pass a string variable in if statements. Commented Sep 12, 2015 at 17:43
  • driver just doesn't get initialized maybe your @BeforeTest public void chrome_extension() does not get executed Commented Sep 12, 2015 at 18:10

4 Answers 4

1

You can download "chromedriver.exe" then add this to your project. You no need to use this System.setProperty("webdriver.chrome.driver", "chromedriver.exe");. You can use ChromeDriver driver = new ChromeDriver(); If still get error, please provide permission for the file "chromedrive.exe".

Sign up to request clarification or add additional context in comments.

Comments

0

You should try instantiating your object. This should remove the error being presented.

Comments

0

Change the code to like this , may be it works.

@Test (priority = 2)
    public void sortIn() throws Exception
    { `enter code here
        sortObj = new KnowledgeBase();
        sortObj.sortBy("Most Recent");
    }

Comments

0

Try something like below. i created the WebDriver object outside @Test annotation and instantiated it in @Test as.

public class TestNGClass
{
    WebDriver driver;
    public String baseUrl = "http://newtours.demoaut.com/"; 
    @Test
    public void verifyHomepageTitle()
    {
        driver = new ChromeDriver();
        driver.get(baseUrl);
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = driver.getTitle();
        Assert.assertEquals(actualTitle, expectedTitle);
        driver.quit();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.