3

I have the following java class (implements page object pattern)

package core.pageObjects;

import org.openqa.selenium.*;

public class ConsultaClientePorDocumento {

    private WebDriver driver;

    public ConsultaClientePorDocumento(WebDriver d){
        this.driver = d;
    }

    public WebElement cancelarButton = driver.findElement(By.id("Cancelar"));
}

Then I try to use it in my test like this:

import core.pageObjects.*;

ConsultaClientePorDocumento consultaCPD = new ConsultaClientePorDocumento(driver);

And I get the following error:

java.lang.NullPointerException
at core.pageObjects.ConsultaClientePorDocumento.<init>(ConsultaClientePorDocumento.java:16)

What am I doing wrong?

rolfl
  • 17,539
  • 7
  • 42
  • 76
user3245906
  • 175
  • 9

2 Answers2

7

Fields are initialized prior to the constructor's body (except for any explicit or implicit call to a superclass constructor). This means that driver is not initialized by the constructor yet when cancelarButton is initialized; it still has the default value of null.

Place the cancelarButton initialization code in the constructor after initializing driver to ensure that driver is initialized before it's needed in the initialization code for cancelarButton.

private WebDriver driver;

public ConsultaClientePorDocumento(WebDriver d){
    this.driver = d;
    this.cancelarButton = driver.findElement(By.id("Cancelar"));
}

public WebElement cancelarButton;

The JLS, Section 12.5, specifies this behavior:

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

I have emphasized the last step, which is to execute the rest of the constructor.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • I see, thanks! The thing is that each WebElement of the page should be initialize when the test requires them and not in the page contructor phase. So I guess I will need some sort of method to initialize the WebElement when the element is called. like so: public WebElement cancelarButton(){ WebElement cancelarButton = driver.findElement(By.id("id"); return cancelarButton } – user3245906 Oct 07 '14 at 21:52
3

You're trying to use driver before it's set by the constructor. Move the initialization of cancelarButton into the constructor:

public ConsultaClientePorDocumento(WebDriver d){
    this.driver = d;
    cancelarButton = driver.findElement(By.id("Cancelar"));
}

public WebElement cancelarButton;

It doesn't matter where the fields are in the file, they will all be evaluated before the constructor is called.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23