出现 Java NullPointerException
错误。这是一个实践项目。谁能解释一下异常的原因吗?
2018 年 11 月 10 日下午 6:47:58 org.openqa.selenium.remote.ProtocolHandshake
createSession INFO:检测到的方言:线程“main”中的 W3C 异常
java.lang.NullPointerException 位于
Flipkartdemo.Flipkartmethods.closelogin(Flipkartmethods.java:31) at
Flipkartdemo.Mainflipkart.main(Mainflipkart.java:9)
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Flipkartmethods {
public FirefoxDriver driver;
public void invokefirefoxflipkart () {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\A\\eclipse-workspace\\libs\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
Dimension dim = new Dimension(640,480);
driver.manage().window().setSize(dim);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get("https://www.flipkart.com/");
// driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[@class='_2AkmmA _29YdH8']")).click();
}
public void exitfirefoxflipkart () {
driver.quit();
}
public void searchlaptop () {
driver.findElement(By.xpath("//input[@class='LM6RPg']")).sendKeys("laptop");
driver.findElement(By.xpath("//div[contains(text(),'Popularity')]")).click();
}
}
这是主要方法;只是一些简单的函数调用:
public class Mainflipkart {
public static void main(String[] args) {
Flipkartmethods fff = new Flipkartmethods();
fff.invokefirefoxflipkart();
fff.searchlaptop();
fff.exitfirefoxflipkart();
}
}
最佳答案
最可能的原因是 driver
在 invokefirefoxflipkart
方法中被重新定义,因此在 exitfirefoxflipkart()
中对它的引用> 和 searchlaptop()
使用实例变量,但 invokefirefoxflipkart
使用本地变量。
修复方法是将 invokefirefoxflipkart()
方法中的行更改为:
driver = new FirefoxDriver();
我在代码异常(exception)中添加了注释。
public class Flipkartmethods {
// This definition is creating an instance variable visible in all methods
public FirefoxDriver driver;
public void invokefirefoxflipkart () {
// This definition which creates the instance of the `driver` is
// local to the invokefirefoxflipkart. Therefore its use is limited
// to this method's scope.
//
// To fix the issue, change the line to be:
// driver = new FirefoxDriver();
// which will eliminate the definition in the local scope, and use
// the instance variable
FirefoxDriver driver = new FirefoxDriver();
...
public void exitfirefoxflipkart () {
// This call is using the instance variable, but it has never been
// initialized to a value, due to the local definition in the
// invokefirefoxflipkart();
// Instance variables that are objects default to null, so this
// will be an NPE
driver.quit();
}
public void searchlaptop () {
// This is also using the instance variable; same issue as described in the previous method
driver.findElement(By.xpath("//input[@class='LM6RPg']")).sendKeys("laptop");
关于java - 对于 Flipkart 项目,获取 wiered 异常 Null_Pointer_Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240322/