0

In my test framework, I have a DriverInitializer class that initializes the WebDriver and WebDriverWait instances (instead of initializing it on every class). The page object classes extend this class to access the WebDriver.

I'm getting a nullpointer exception thrown while trying to access the WebDriverWait instance. See void method init_webdriver_wait below:

public class DriverInitializer {

    protected WebDriver driver;
    protected WebDriverWait wdw;
    protected final long timeInSeconds = 30;

    public DriverInitializer(WebDriver driver) {
        this.driver = driver;
    }

    public WebDriver getDriver() {
        return driver;
    }

    public PropertyConfig goToHome() {
        PropertyConfig pc = new PropertyConfig(driver);
        pc.getAppUrl();

        return pc;
    }

    public void init_webdriver_wait() {
        try {
            wdw =  new WebDriverWait(driver, timeInSeconds);    
        }
        catch(Exception e) {
           // This error message is being triggered
           // and always says that the WebDriverWait instance is null.
           System.out.println("Error in DriverInitializer " + e.getMessage());
        }       
    }
}

In my second class, the LandingPage page factory class, I extended the DriverInitializer class:

 public class LandingPage extends DriverInitializer {       

    @FindBy(how=How.LINK_TEXT, using="REGISTER")
    WebElement register_link;

    @FindBy(how=How.LINK_TEXT, using="LOGIN")
    WebElement login_link;

    public LandingPage(WebDriver driver) {
        super(driver);
        PageFactory.initElements(driver, this);
    }

    public void locate_register_link() throws InterruptedException {

        try {
            init_webdriver_wait();      
            if(wdw != null) {
                // Sample check if the WebDriverWait instance is null.
                wdw.until(ExpectedConditions.elementToBeClickable(register_link));
                Thread.sleep(3000); 
            }
            else {
                // This error message is triggered.
                // It says that the WebDriverWait instance is null.
                System.out.println("WDW is null -> LandingPage.java");
            }
        }
        catch(Exception e) {
            System.out.println("Error in LandingPage => " + e.getMessage());
        }
    }
}

Why does it always return a null pointer exception on my WebDriverWait instance?

1
  • Can you add the callstack?
    – FDM
    Commented Apr 19, 2017 at 12:31

1 Answer 1

1
`try{
         init_webdriver_wait();
         if(wdw != null){ // sample check if the webdriverwait instance is null
            wdw.until(ExpectedConditions.elementToBeClickable(register_link));
            Thread.sleep(3000);`

My guess would be that wdw is null because you never set a value to that variable. Could you try: WebdriverWait wdw = init_webdriver_wait();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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