![](/img/trans.png)
[英]How to access from “onCreate()” an Arraylist variable in another method of same class?
[英]How to access a variable stored in another method in java in the same class
我需要在第二种方法中访问变量“值”。 我尝试将其设为实例变量,但它只是作为 null 返回。
public class RateFellowUnsatisfactoryPage extends PageObject {
@FindBy(xpath = "actual xpath")
private WebElementFacade checkbox;
@FindBy(xpath = "actual xpath")
private WebElementFacade fellowRated;
private String value;
public void selects_first_checkbox() {
value = checkbox.getValue();
System.out.println(value);
checkbox.click();
}
public void verifyFellowDisplayed() {
String fellow = fellowRated.getTextValue();
System.out.println(fellow);
assertThat(fellow.equalsIgnoreCase(value));
System.out.println(value);
}
}
你在做什么看起来是正确的。 您确定在您为此 RateFellowUnsatisfactoryPage 实例调用 verifyFellowDisplayed 时已经调用了 selects_first_checkbox 吗? 显然,如果还没有设置的话,还是null。
假设它已被调用,它是否在同一个线程中被调用? Java 有时会优化掉对成员变量的引用。 尝试将其声明为 volatile,只要您使用(我认为)Java 8 或更高版本,它就应该修复它。 如果这不能解决问题,请尝试将其声明为 AtomicReference,您将有一个额外的步骤来访问它。 对不起,但我忘了具体是什么。 AtomicReference 的要点是它不会优化对它的任何引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.