java - 调用构造函数时的 NPE

标签 java selenium pageobjects

我有以下 java 类(实现页面对象模式)

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)

我做错了什么?

最佳答案

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

放置cancelarButton初始化后构造函数中的初始化代码driver确保drivercancelarButton 的初始化代码中需要它之前被初始化.

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.

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

关于java - 调用构造函数时的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245769/

相关文章:

java - 使用 PageFactory 时处理 StaleElementReferenceException

java - 如何保证两个微服务之间的接口(interface)不被破坏?

python - 为什么 Selenium 只获取页面上第一个 ToolTip 的文本?

java - 如何使用 java 在 Selenium WebDriver 项目中表示页面对象导航

java - 尝试查找 WebElement 时出现 NullpointerException

c# - 如何简化此方法的表达式参数?

python - 返回一个只在某些时候使用的对象

java - 自 1.3 以来,Java 核心有哪些新增内容?

java - 关闭从eclipse启动的远程调试的tomcat

java - 将应用程序部署到 AWS Lambda 会引发 Postgres 驱动程序错误