java – 在调用构造函数时的NPE
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在调用构造函数时的NPE,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3028字,纯文字阅读大概需要5分钟。
内容图文
![java – 在调用构造函数时的NPE](/upload/InfoBanner/zyjiaocheng/756/725d4b37454347ccb7bd82962dbd0767.jpg)
我有以下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)
我究竟做错了什么?
解决方法:
字段在构造函数体之前初始化(除了对超类构造函数的任何显式或隐式调用之外).这意味着当初始化cancelarButton时,构造函数尚未初始化驱动程序;它仍然具有默认值null.
在初始化驱动程序之后将cancelarButton初始化代码放在构造函数中,以确保在cancelarButton的初始化代码中需要初始化驱动程序.
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:
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
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.
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.
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.
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全部内容,希望文章能够帮你解决java – 在调用构造函数时的NPE所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。