0

I am getting null pointer exception when selenium tries to launch the browser.

  1. I have already checked step Definition.
  2. Selenium OpenBrowser Method.
  3. Chrome browser version. ( I have driver 87.04 matches with version of my chrome browser)

I am still facing this issue can you please help.. please find below details:

  1. Step Definition:
   package steps;

import cucumber.api.PendingException;
import pagesAction.CarsGuideHomePageActions;
import pagesAction.CarsSearchPageActions;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import java.util.List;

import Utilities.SeleniumDriver;
//import cucumber.api.DataTable;

public class SearchCarsSteps {
    
    CarsGuideHomePageActions CarsGuideHomePageActions = new CarsGuideHomePageActions();
    CarsSearchPageActions CarsSearchPageActions = new CarsSearchPageActions();
    
    @Given("^I am on the Home Page \"([^\"]*)\" of CarsGuide Website$")
    public void i_am_on_the_home_page_of_cars_guide(String webSiteURL) throws Throwable {
        
        SeleniumDriver.openPage(webSiteURL);
       
    }

    @When("^I move to Menu menu$")
    public void i_move_to_Menu_menu(List<String> list) throws Throwable {
     
        String menu = list.get(1);
        System.out.println("Menu Selected is: "+menu);
        CarsGuideHomePageActions.moveToBuyPlusSaleMenu();
        
    }

    @And("^I clicked on \"([^\"]*)\" link$")
    public void i_clicked_on_link(String SearchCars) throws Throwable {
        CarsGuideHomePageActions.clickOnSearchCars();  
    }

    @And("^select carbrand as \"([^\"]*)\" from AnyMake Dropdown$")
    public void select_carbrand_as_from_AnyMake_Dropdown(String carBrand) throws Throwable {
        CarsSearchPageActions.selectCarMake(carBrand);
    }

    @And("^select carModel as \"([^\"]*)\" from selectModel dropdown$")
    public void select_carModel_as_from_selectModel_dropdown(String CarModel) throws Throwable {
        CarsSearchPageActions.selectCarModel(CarModel);
    }

    @And("^select location as \"([^\"]*)\" from selectLocation dropdown$")
    public void select_location_as_from_selectLocation_dropdown(String CarLocation) throws Throwable {
        CarsSearchPageActions.selectCarLocation(CarLocation);
    }

    @And("^select price as \"([^\"]*)\" from priceRange Dropdown$")
    public void select_price_as_from_priceRange_Dropdown(String CarPrice) throws Throwable {
        CarsSearchPageActions.selectCarPrice(CarPrice);
    }

    @And("^Click on Find my next car button$")
    public void click_on_Find_my_next_car_button() throws Throwable {
        CarsSearchPageActions.clickOnFindMyNextCarButton();
    }

    @Then("^I should see list of searched cars$")
    public void i_should_see_list_of_searched_cars() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @And("^the page tile should be \"([^\"]*)\"$")
    public void the_page_tile_should_be(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }


}

2. Runner:

package runners;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.testng.annotations.BeforeClass;

import com.cucumber.listener.ExtentCucumberFormatter;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

//import io.cucumber.testng.AbstractTestNGCucumberTests;
@CucumberOptions(
        plugin = {"json:target/RunCuke/cucumber.json", "pretty", "html:target/RunCuke/cucumber.html","com.cucumber.listener.ExtentCucumberFormatter"},
        features = "src/test/resources/features",
        glue = "steps",
        tags = {"@Search-Cars"}        
        )
public class RunCuke extends AbstractTestNGCucumberTests{
    
    @BeforeClass
    public static void setup() {
        // Initiates the extent report and generates the output in the output/Run_<unique timestamp>/report.html file by default.
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_hhmmss");
        Date curDate = new Date();
        String strDate = sdf.format(curDate);
        String fileName = System.getProperty("user.dir")+"\\target\\Extent_Reports\\" + strDate+".html";
        File newFile = new File(fileName);
        ExtentCucumberFormatter.initiateExtentCucumberFormatter(newFile,false);
        //static report name
        //ExtentCucumberFormatter.initiateExtentCucumberFormatter(new File("F:\\cucumber-testing-master\\ExtenReports\\extentreports.html"),false);
        // Loads the extent config xml to customize on the report.
        ExtentCucumberFormatter.loadConfig(new File("src/test/resources/extent-config.xml"));

        // User can add the system information as follows
        ExtentCucumberFormatter.addSystemInfo("Browser Name", "Chrome");
        ExtentCucumberFormatter.addSystemInfo("Browser version", "v31.0");
        ExtentCucumberFormatter.addSystemInfo("Selenium version", "v2.53.0");

        // Also you can add system information using a hash map
        Map systemInfo = new HashMap();
        systemInfo.put("Cucumber version", "v1.2.3");
        systemInfo.put("Extent Cucumber Reporter version", "v1.1.0");
        ExtentCucumberFormatter.addSystemInfo(systemInfo);
    }

}

3. SeleniumDriver Utility where at line-32 I am getting null pointer exception.

package Utilities;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumDriver {
    
    public static SeleniumDriver seleniumDriver;
    
    //initialize webdriver
    public static WebDriver driver;
    
    private static WebDriverWait waitDriver;
    public final static int TIMEOUT= 30;
    public final static int PAGE_LOAD_TIMEOUT= 50;
    
    private SeleniumDriver() {
        
    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+ "\\src\\test\\resources\\executables\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        waitDriver = new WebDriverWait(driver,TIMEOUT);
        driver.manage().timeouts().implicitlyWait(TIMEOUT, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(TIMEOUT, TimeUnit.SECONDS);
        
    }
    
    public static void openPage(String url) {
        driver.get(url);
    }
    
    public static WebDriver getDriver() {
        return driver;
    }
    
    public static void setDriver() {
        if(seleniumDriver== null) {
            seleniumDriver = new SeleniumDriver();
        }
    }
    
    public static void tearDown() {
        if(driver!= null) {
            driver.close();
            driver.quit();
        }
        seleniumDriver= null;
    }
    
    

}
  1. Before Action
    package runners;
    
    import Utilities.SeleniumDriver;
    import cucumber.api.java.Before;
    
    public class BeforeActions {
        
        @Before
        public static void setup() {
            SeleniumDriver.setUpDriver();
        }
    
    }

And the exception is below:

[RemoteTestNG] detected TestNG version 6.9.10
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNG] Running:
  C:\Users\ACER\eclipse-workspace\seleniumCucumber\src\test\resources\runner\testng.xml

@Search-Cars
Feature: 
  acceptance tesing is to validate search car funcationality is working fine
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by cucumber.deps.com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/ACER/.m2/repository/info/cukes/cucumber-jvm-deps/1.0.5/cucumber-jvm-deps-1.0.5.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of cucumber.deps.com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

  @Search-Cars-Positive
  Scenario: validat the search car page                                                     [90m# searchCar.feature:6[0m
    [31mGiven [0m[31mI am on the Home Page "[0m[31m[1mhttps://www.carsguide.com.au[0m[31m" of CarsGuide Website[0m         [90m# SearchCarsSteps.i_am_on_the_home_page_of_cars_guide(String)[0m
      [31mjava.lang.NullPointerException
        at Utilities.SeleniumDriver.openPage(SeleniumDriver.java:32)
        at steps.SearchCarsSteps.i_am_on_the_home_page_of_cars_guide(SearchCarsSteps.java:24)
        at ?.Given I am on the Home Page "https://www.carsguide.com.au" of CarsGuide Website(searchCar.feature:7)
      [0m
    [36mWhen [0m[36mI move to Menu menu[0m                                                                [90m# SearchCarsSteps.i_move_to_Menu_menu(String>)[0m
    [36mAnd [0m[36mI clicked on "[0m[36m[1mSearch cars[0m[36m" link[0m                                                     [90m# SearchCarsSteps.i_clicked_on_link(String)[0m
    [36mAnd [0m[36mselect carbrand as "[0m[36m[1mBMW[0m[36m" from AnyMake Dropdown[0m                                      [90m# SearchCarsSteps.select_carbrand_as_from_AnyMake_Dropdown(String)[0m
    [36mAnd [0m[36mselect carModel as "[0m[36m[1m1 Series[0m[36m" from selectModel dropdown[0m                             [90m# SearchCarsSteps.select_carModel_as_from_selectModel_dropdown(String)[0m
    [36mAnd [0m[36mselect location as "[0m[36m[1mAustralia[0m[36m" from selectLocation dropdown[0m                         [90m# SearchCarsSteps.select_location_as_from_selectLocation_dropdown(String)[0m
    [36mAnd [0m[36mselect price as "[0m[36m[1m$1,000[0m[36m" from priceRange Dropdown[0m                                   [90m# SearchCarsSteps.select_price_as_from_priceRange_Dropdown(String)[0m
    [36mAnd [0m[36mClick on Find my next car button[0m                                                    [90m# SearchCarsSteps.click_on_Find_my_next_car_button()[0m
    [36mThen [0m[36mI should see list of searched cars[0m                                                 [90m# SearchCarsSteps.i_should_see_list_of_searched_cars()[0m
    [36mAnd [0m[36mthe page tile should be "[0m[36m[1mBmw 1 Series Under 1000 for sale Melbourne VIC| carsguide[0m[36m"[0m [90m# SearchCarsSteps.the_page_tile_should_be(String)[0m

[31mFailed scenarios:[0m
[31msearchCar.feature:6 [0m# Scenario: validat the search car page

1 Scenarios ([31m1 failed[0m)
10 Steps ([31m1 failed[0m, [36m9 skipped[0m)
0m0.293s

java.lang.NullPointerException
    at Utilities.SeleniumDriver.openPage(SeleniumDriver.java:32)
    at steps.SearchCarsSteps.i_am_on_the_home_page_of_cars_guide(SearchCarsSteps.java:24)
    at ?.Given I am on the Home Page "https://www.carsguide.com.au" of CarsGuide Website(searchCar.feature:7)


===============================================
testCucumber
Total tests run: 1, Failures: 1, Skips: 0
===============================================
3

You don't actually initialize the driver before you call it.

Your SeleniumDriver.openPage() call happens without any of the initialization code for the driver being called.

Your code:

public static SeleniumDriver seleniumDriver;

//initialize webdriver
public static WebDriver driver;

does not actually initialize the driver - it merely declares the variable. Your code should read:

public static WebDriver driver;
public static SeleniumDriver seleniumDriver = new SeleniumDriver();

Alternatively, you could call SeleniumDriver.setDriver(); before you call openPage()

Both methods will initialize the driver.

  • Hi Kate, Missed to add the code of BeforeAction which will invoke setDriver() in the question. Now it is added but still i am facing same issue, not sure what is the problem – Hemant Varhekar Dec 3 '20 at 7:00
  • @HemantVarhekar - the code you need to call is SeleniumDriver.setDriver() not SeleniumDriver.setUpDriver() – Kate Paulk Dec 3 '20 at 13:37
  • thanks got the issue – Hemant Varhekar Dec 9 '20 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.