java - 对于 Flipkart 项目,获取 wiered 异常 Null_Pointer_Exception

标签 java selenium selenium-webdriver selenium-firefoxdriver

出现 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();              
    }   
}

最佳答案

最可能的原因是 driverinvokefirefoxflipkart 方法中被重新定义,因此在 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/

相关文章:

java - 在java中使用PDFBox,如何创建带有文本的可见数字签名

python - 以前未见过的 Web 元素引用

javascript - Selenium - 如何获得被屏蔽的图像?

c# - MailKit:如何迭代最近的电子邮件以获取具有给定主题的电子邮件

java - 克隆 "By": Defensive Programming with Selenium

selenium - driver.quit() 不会关闭浏览器窗口

python - 如何在 Python WebDriver 中等待 CSS 和 XPath 选择器的组合?

java - 如何在一种方法和另一种方法中使用声明的变量。

java - 在只读服务器上使用java在Mysql中创建临时表导致错误

java - Hibernate 无法与 Sql Server 2008 一起使用