1

Here, I am using page object model and I want to pass the driver to other classes.But i am getting null pointer exception while launch the website (driver.get("")).

This is my base class

public class BaseClass {
public WebDriver driver;
public Logger logger = Logger.getLogger(Common.class.getPackage().getName());
public void startBrowser() {
    if(driver == null) {
        System.setProperty("webdriver.chrome.driver", "desktop/chromedriver.exe");
        driver = new ChromeDriver();
    }
}
public void quitBrowser() {
    driver.quit();
}

}

and then this is my runner class:

public class TestRunnerTestNG extends AbstractTestNGCucumberTests {
BaseClass a;
@BeforeClass
public void launch()
{
    a = new BaseClass();
    a.startBrowser();
}

@AfterClass
public void tearBrowser()
{
    a.quitBrowser();
}

}

Here, I am starting the browser using Beforeclass annotation and quit the browser using afterClass annotation.

and the following class is my page Object class: and here I have the method for launch the url:

public class SignIn extends BaseClass {


public SignIn(WebDriver driver) {

    this.driver = driver ;
    PageFactory.initElements(driver, this);
}

//Locators
@FindBy(id = "email")
private WebElement user_Email;

@FindBy(id = "password")
private WebElement user_Password;

@FindBy(xpath = "//span[text()='Sign In']")
private WebElement signIn_Btn;

public void landing() {

  driver.get("https://***************"); <<<< Here I am getting the null pointer exception.

}


public void signInPageGUI()
{
    boolean checkSignInTextGUI = waitElement(signInText);
    Assert.assertTrue(checkSignInTextGUI);
    boolean CheckEmailField = waitElement(user_Email);
    Assert.assertTrue(CheckEmailField);
    boolean checkPwdField = waitElement(user_Password);
    Assert.assertTrue(checkPwdField);
}

private void emailField(String emailName) {

    user_Email.sendKeys(emailName);
}

private void passwordField(String password) {

    user_Password.sendKeys(password);
}

}

and the final code is my step definition class and this is place I am calling the code.

public class LoginPage {
WebDriver driver ;

@Given("user landed to the yoco URL {string}")
public void landedOnYoCo(String string) {

    System.out.println("print the string" +string);

    System.out.println("driver value is " );

    SignIn logIN = new SignIn(driver);

    logIN.landing();

}

}

Here, Only I am calling the landing method to launch the website.

and The error is:

java.lang.NullPointerException
at pageObject.SignIn.landing(SignIn.java:83)
at stepDefs.LoginPage.landedOnYoCo(LoginPage.java:32)
at ✽.user landed to the yoco URL "https://my.yocoboard.com"(file:///Users/vinoth/Git/YoCoAutomation/src/test/resources/logIN.feature:7)
Vinoth BS
  • 21
  • 3
  • 1
    In `landedOnYoCo` you pass driver to SignIn without initializing it. Without initialization, driver is `null`. Then when you call `logIN.landing` SignIn's `this.driver` is null so you get a NullPointerException because you are trying to call a method on `null`. I think you should initialize driver before calling SignIn's constructor. – Nathan Mills Sep 26 '20 at 02:46

1 Answers1

0

Call startBrowser before calling landing to initialize driver.

public void landedOnYoCo(String string) {

    System.out.println("print the string" +string);

    System.out.println("driver value is " );

    SignIn logIN = new SignIn(driver);

    logIN.startBrowser();

    logIN.landing();

}
Nathan Mills
  • 1,034
  • 4
  • 11