I am new to Cucumber and was trying a basic cucumber test but getting **Java null pointer exception* . Have tried to initialize the driver in base class and using inheritance called the driver object in my util class. Could anyone please help in letting me know , where i went wrong .
Below is the result and code.
Result:
java.lang.NullPointerException
at stepDefinations.landingPageSD.navigate_to_Site(landingPageSD.java:51)
**BASE CLASS**
package pages;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;
public class baseTest {
public static WebDriver driver;
public static WebDriver getDriver() {
return driver;
}
public static void setDriver(WebDriver driver) {
baseTest.driver = driver;
}
public static void getDriver(WebDriver driver) {
baseTest.driver = driver;
}
public static Properties prop;
public static WebDriver initializeDriver() throws IOException
{
prop= new Properties();
FileInputStream fis=new FileInputStream("D:\\CucumberProject\\original\\E2EProjectCucumber\\src\\main\\java\\browsers\\data.properties");
prop.load(fis);
String browserName=prop.getProperty("browser");
System.out.println(browserName);
if(browserName.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "D:\\CucumberProject\\original\\E2EProjectCucumber\\src\\main\\java\\browsers\\chromedriver.exe");
driver= new ChromeDriver();
}
else if (browserName.equals("firefox"))
{
System.setProperty("webdriver.chrome.driver", "D:\\CucumberProject\\original\\E2EProjectCucumber\\src\\main\\java\\browsers\\geckodriver.exe");
driver= new FirefoxDriver();
}
**UTIL CLASS**
package pages;
import java.io.IOException;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GenericUtilsTest extends baseTest{
public WebDriverWait wait;
public Actions actions;
public boolean navigateToURL(String URL) throws Exception {
System.out.println("Navigating to: " + URL);
System.out.println("Thread id = " + Thread.currentThread().getId());
try {
driver.navigate().to(URL);
} catch (Exception e) {
System.out.println("URL did not load: " + URL);
throw new Exception("URL did not load");
}
return false;
}
Feature: Login into Application
Scenario Outline: Positive test validating login
Given Initialize the browser with chrome
**And Navigate to "https://www.makemytrip.com/" Site**
Then Make my trip landing page is displayed
And Click on Login link in home page to land on Secure sign in Page
When User enters <username> and <password> and logs in
Then Verify that user is succesfully logged in
**LandingPage**
package stepDefinations;
import org.junit.Assert;
import cucumber.api.PendingException;
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 pages.LandingPageTest;
import pages.LoginPageTest;
import pages.baseTest;
import pages.GenericUtilsTest;
import pages.PortalHomePageTest;
public class landingPageSD {
private LandingPageTest landingPage;
private GenericUtilsTest genericPage;
private LoginPageTest loginPage;
private PortalHomePageTest homePage;
public landingPageSD(LandingPageTest landingPage, GenericUtilsTest genericPage, LoginPageTest loginPage,
PortalHomePageTest homePage) {
super();
this.landingPage = landingPage;
this.genericPage = genericPage;
this.loginPage = loginPage;
this.homePage = homePage;
}
@Given("^Initialize the browser with chrome$")
public void initialize_the_browser_with_chrome() throws Throwable {
genericPage .initializeDriver();
}
@Given("^Navigate to \"(.*?)\" Site$")
public void navigate_to_Site(String arg1) throws Throwable {
Assert.assertTrue("Customer is not able to navigate to landing page",genericPage.navigateToURL(arg1));
}