Getting NullPointerException error using WebDriverWait wait = new WebDriverWait(driver, 20);

I am new to WebDriver. I am trying to write a reusable method to enter text on a page which has multiple text fields. HTML looks like this <input name=”username”>, <input name=”password”>.

Below is the method I wrote to reuse
public class EnterTextMethod {

public static WebDriver driver;
public EnterTextMethod enterText(String inputField, String text) {
String inputLocator = “input[name='” + inputField + “‘]”; ( I am doing this to reuse this method. Does  WebDriver allow this?)
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(inputLocator))).sendKeys(text);
return this;
}

@Test –  Below is my test where  I am trying to enter text

login.enterText(“username”, “amazon”);
login.enterText(“password”, “Prime25″);

When I run my test method, I keep getting NullPointerException error at <WebDriverWait wait = new WebDriverWait(driver, 20);>so I might be doing something wrong here. I can’t seem to figure out what is the root cause.

Has anyone run into this issue ? I am not sure what I am doing wrong here. Stuck here from couple of days..

Thanks

Add Comment
5 Answer(s)

I’m not sure that this code is correct

String inputLocator = “input[name=’” + inputField + “‘]”; ( I am doing this to reuse this method. Does  WebDriver allow this?)

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(inputLocator))).sendKeys(text);

 

victor Reviewer Answered on July 21, 2015.
Add Comment

In Selenium RC,  it worked fine. So, I thought of using it in WebDriver as well.  Maybe Selenium WebDriver doesn’t allow, not sure.

amanandhar Train Answered on July 21, 2015.
Add Comment

public EnterTextMethod enterText(String inputField, String text)
what for you need to return EnterTextMethod object?

I think problem in this method

victor Reviewer Answered on July 21, 2015.
Add Comment

Where exactly are you initializing driver? as per this code its not being initialized and is null when you try to create a webdriver wait on it.

Virender

Virender Singh Professor Answered on July 22, 2015.
Add Comment

Thanks to all

I thought I posted my reply,  apparently  not. I am extending multiple classes, which is why the webdriver was set to null. I initialized the variable and got it is working.

amanandhar Train Answered on July 30, 2015.
Add Comment

Your Answer

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