[英]How to access a variable stored in another method in java in the same class
I need to access the variable "value" in the second method.我需要在第二种方法中访问变量“值”。 I tried making it an instance variable but it just returned as a null.
我尝试将其设为实例变量,但它只是作为 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);
}
}
What you're doing looks correct.你在做什么看起来是正确的。 Are you sure that selects_first_checkbox has been called already at the point that you're calling verifyFellowDisplayed for this instance of RateFellowUnsatisfactoryPage?
您确定在您为此 RateFellowUnsatisfactoryPage 实例调用 verifyFellowDisplayed 时已经调用了 selects_first_checkbox 吗? Obviously, if it has not yet been set, it will still be null.
显然,如果还没有设置的话,还是null。
Assuming it has been called, is it being called in the same thread?假设它已被调用,它是否在同一个线程中被调用? Java will sometimes optimize away references to member variables.
Java 有时会优化掉对成员变量的引用。 Try declaring it volatile, which should fix it as long as you're using (I think) Java 8 or higher.
尝试将其声明为 volatile,只要您使用(我认为)Java 8 或更高版本,它就应该修复它。 If that doesn't fix it, try declaring it to be AtomicReference and you'll have an extra step to access it.
如果这不能解决问题,请尝试将其声明为 AtomicReference,您将有一个额外的步骤来访问它。 Sorry, but I forget exactly what.
对不起,但我忘了具体是什么。 The point of AtomicReference is that it will not optimize away any reference to it.
AtomicReference 的要点是它不会优化对它的任何引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.