0

The code runs fine but I created a new page object for a new page to test but I am getting this null pointer exception. Here are the two class I believe I am having issues with.

public class LoggedInPageSteps {
    WebDriver driver;   
    private Logger logger = Logger.getLogger(HomePage.class);
    LoggedInPage loggedInPage;

    @Then ("^I check the my account info tag$")
    public void verifyLoggedInPage() {
        logger.info("Inside loggedinpagesteps verifyloggedin function.");
        logger.info(loggedInPage.getAcctInfoTag());
        loggedInPage.checkLoggedIn();
        //Commons.check(loggedInPage.getAcctInfoTag().equals("My Account Information"), driver, 
        "Title doesnt match");
    
    }

and

public class LoggedInPage {

    @FindBy(tagName = "h1")
    WebElement acctInfoTag;

    WebDriver driver;
    //LoggedInPage loggedInPage;

    private Logger logger = Logger.getLogger(LoginPage.class);


    public LoggedInPage(WebDriver driver) {
        this.driver = driver; 
        PageFactory.initElements(driver, this);
    }

    public String getAcctInfoTag() {
        logger.info("insdie LoggedInPage funcrion getAccountInfoTag().");
        return Commons.getElementText(driver, acctInfoTag, 5);
    }

    public void checkLoggedIn() {
        logger.info("Inside LoggedInPage inside function CheckLoggedIn.");
        logger.info(Commons.getElementText(driver, acctInfoTag, 5));
        Commons.check(Commons.getElementText(driver, acctInfoTag, 5).equals("My Account 
        Information"), driver, "Not on logged in page.");
    }

}

Is it a webdriver issue? I am new to automation testing so forgive me if it was a simple mistake.

  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jrook Sep 23 '20 at 20:31

1 Answers1

0

You are never instantiating loggedInPage. Create the follinwing constructor in LoggedInPageSteps to have Cucumber do it for you with dependency injection:

public LoggedInPageSteps(LoggedInPage loggedInPage) {
  this.loggedInPage = loggedInPage;
}

You'll also have to add io.cucumber.picocontainer as a development dependency.

Aslak Hellesøy
  • 1,191
  • 6
  • 9