NullPointerException on FluentWait

I’m after some help with a scenario I have(see code below:

public class WaitCommands {
public static void main(String[] args) {

// TODO Auto-generated method stub

System.setProperty("webdriver.gecko.driver", "C:\\Users\\Andrew.Tilston.AD\\Documents\\Visual Studio 2013\\Projects\\Selenium\\Drivers\\geckodriver.exe");

WebDriver driver = new FirefoxDriver();

driver.get("http://toolsqa.wpengine.com/automation-practice-switch-windows/");
// FLUENTWAIT

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);

wait.pollingEvery(250, TimeUnit.MILLISECONDS);

wait.withTimeout(2, TimeUnit.SECONDS);
Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>()

{

public Boolean apply(WebDriver arg0) {

WebElement element = arg0.findElement(By.id("colorVar"));

String color = element.getAttribute("color");

System.out.println("The color of the button is " + color);

if(color.equals("red"))

{

return true;

}

return false;

}

};

wait.until(function);
driver.quit();

}

}

However, when I run the above code I get the below error in the output

The color of the button is null
Exception in thread “main” java.lang.NullPointerException

I’m struggling to work out why the color attribute cannot be located. Any assistance would be appreciated

Add Comment
  • 2 Answer(s)

    Hi ,

    It looks like while you are giving a call to the Function “function”, you are not passing the driver and the boolean value as the argument to it which ideally its syntax declaration has.

    Try passing the driver and the boolean argument to it and then run it.

    Not too sure but the first look gave me this feeling that this could be the issue since you are not passing the driver to the function, it cannot locate the element and the attribute of that element eventually and hence the color attribute cannot be located.

    Hope it works, fingers crossed. If not then may be we can re-think on it 🙂

    Default Answered on July 7, 2017.

    Hi there, many thanks for the detailed response. I understand conceptually your response but am having trouble putting it into practise. Would it be possible to provide an example of the answer you have provided in a code snippet, so I can then step through the code?

    Thanks again

     

    Andy

    on July 7, 2017.
    Add Comment

    Hi there, many thanks for the detailed response. I understand conceptually your response but am having trouble putting it into practise. Would it be possible to provide an example of the answer you have provided in a code snippet, so I can then step through the code?

    Thanks again

     

    Andy

    Train Answered on July 7, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.