2014-10-07 125 views
3

我有以下的Java类(实现页面对象模式)NPE在构造函数被调用

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")); 
} 

然后我尝试使用它在我的测试是这样的:

import core.pageObjects.*; 

ConsultaClientePorDocumento consultaCPD = new ConsultaClientePorDocumento(driver); 

我也得到出现以下错误:

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

我在做什么错?

回答

7

字段在构造函数的主体之前被初始化(除了对超类构造函数的任何显式或隐式调用)。这意味着在初始化cancelarButton时尚未由构造函数初始化;它仍然具有默认值null

放置cancelarButton初始化代码在构造函数初始化driver,以确保它需要在初始化代码为cancelarButton之前driver初始化后。

private WebDriver driver; 

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

public WebElement cancelarButton; 

JLS, Section 12.5,指定该行为:

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.

我已经强调了最后一步,这是执行构造函数的其余部分。

+0

我明白了,谢谢! 问题是,当测试需要它们而不是页面构造器阶段时,页面的每个WebElement应该被初始化。 所以我想我会需要某种方法来初始化元素被调用时的WebElement。像这样: public WebElement cancelarButton(){ WebElement cancelarButton = driver.findElement(By.id(“id”); return cancelarButton } – user3245906 2014-10-07 21:52:04

3

您正在尝试使用driver,它是由构造函数设置的。移动的cancelarButton初始化到构造:

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

public WebElement cancelarButton; 

没关系,其中字段是在文件中,他们都将被评估的构造函数被调用之前。