java.lang.NullPointerException when executing cucumber feature file through step definition file using cucumber-junit with Java

11,045

You are seeing NullPointerException as you have declared a instance of WebDriver as in :

WebDriver driver;

But moving forward in the user_is_on_Homepage() function you have initiated another instance of WebDriver as in :

WebDriver driver = new ChromeDriver();

As all your functions will be using the same instance of the WebDriver you need to use the instance of WebDriver you have declared globally.

Solution

Change the line from :

WebDriver driver = new ChromeDriver();

To :

driver = new ChromeDriver();
Share:
11,045

Related videos on Youtube

Ab123
Author by

Ab123

Hello Guys, I am Sr. Automation Test Engineer mostly work with Selenium Webdriver with Java,TestNG,Maven,Jenkins. I am having 5 years of experience in the field of testing including Manual and Automation. Also I have little Experience on SOAP-UI Automation.

Updated on June 04, 2022

Comments

  • Ab123
    Ab123 almost 2 years

    While running a cucumber feature file using junit with Java, I am getting a NULL pointer exception. I am not able to understand why this exception is coming up.
    This is my step definition file, written in java.

    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    import pageObjects.CartPage;
    import pageObjects.Checkoutpage;
    import pageObjects.HomePage;
    import pageObjects.ProductListingPage;
    import cucumber.api.PendingException;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.When;
    
    public class EndtoEndTest {
    
    WebDriver driver;
    
    @Given("^User is on Homepage$")
    public void user_is_on_Homepage() throws Throwable {
    
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.shop.demoqa.com");
        driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
        driver.manage().window().maximize();
    
    
    }
    
    @When("^he searches for \"([^\"]*)\"$")
    public void he_searches_for(String arg1) throws Throwable {
    
        HomePage home = new HomePage(driver);
        home.perform_Search(arg1);
    }
    
    @When("^Choose to buy the first item$")
    public void choose_to_buy_the_first_item() throws Throwable {
        ProductListingPage productListingPage = new ProductListingPage(driver);
        productListingPage.select_Product(0);
        productListingPage.clickOn_AddToCart(); 
    }
    
    @When("^moves to checkout from mini cart$")
    public void moves_to_checkout_from_mini_cart() throws Throwable {
        CartPage cartPage = new CartPage(driver);
        cartPage.clickOn_Cart();
        cartPage.clickOn_ContinueToCheckout();
    }
    
    @When("^enter personal details onn checkout page$")
    public void enter_personal_details_onn_checkout_page() throws Throwable {
    
    
        Checkoutpage checkoutPage = new Checkoutpage(driver);
        checkoutPage.fill_PersonalDetails();    
    }
    
    @When("^select same delivery address$")
    public void select_same_delivery_address() throws Throwable {
        Checkoutpage checkoutPage = new Checkoutpage(driver);
        checkoutPage.check_ShipToDifferentAddress(false);
    }
    
    @When("^select payment method as \"([^\"]*)\" payment$")
    public void select_payment_method_as_payment(String arg1) throws Throwable {
        Checkoutpage checkoutPage = new Checkoutpage(driver);
        checkoutPage.select_PaymentMethod("CheckPayment");
    }
    
    @When("^place the order$")
    public void place_the_order() throws Throwable {
        Checkoutpage checkoutPage = new Checkoutpage(driver);
        checkoutPage.check_TermsAndCondition(true);
        checkoutPage.clickOn_PlaceOrder();
    
        driver.quit();
    }
    }
    

    This is my PageObject File

    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    import org.openqa.selenium.support.PageFactory;
    
    public class HomePage {
    
    WebDriver driver;
    
    public HomePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    
    @FindBy(how=How.XPATH, using="//a[@class='noo-search icon_search']")
    private WebElement click_on_search_icon;
    
    @FindBy(how = How.XPATH, using="//input[@class='form-control']")
    private WebElement enter_data_for_search;
    
    public void perform_Search(String search) {
    
        click_on_search_icon.click();
        enter_data_for_search.sendKeys(search);
        enter_data_for_search.sendKeys(Keys.ENTER);
    
    }
    
    public void navigateTo_HomePage() {
        driver.get("http://www.shop.demoqa.com");
    }
    
    }
    

    **Error stacktrace that I am getting while running the feature file:

    java.lang.NullPointerException
        at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
        at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
        at com.sun.proxy.$Proxy13.click(Unknown Source)
        at pageObjects.HomePage.perform_Search(HomePage.java:27)
        at stepDefinations.EndtoEndTest.he_searches_for(EndtoEndTest.java:39)
        at ✽.When he searches for "dress"(src/test/resources/functionalTest/EndtoEndTest.feature:9)
    

    I am not sure why I am getting Null pointer Exception. Any help would be appreciated. Thanks**

  • Ab123
    Ab123 about 6 years
    Thanks @Manmohan_Singh, it is working now but I have declared Webdriver driver as an instance variable, so instance variable scope is for class level then how it is getting destroyed just after one method. Do you think it is showing weird behavior????
  • Manmohan_singh
    Manmohan_singh about 6 years
    Well Cucumber JVM works in different way than ordinary JVM. The cucumber annotated methods are searched in all classes in the step definitions directory. Only the matching annotated methods are executed. Entire class is not allocated memory during runtime .
  • Haven Shi
    Haven Shi almost 5 years
    Thanks @DebanjanB, this solution works and solved my issue.