Для проекта флипкарта получение исключений Null_Pointer_Exception - PullRequest
0 голосов
/ 10 ноября 2018

Получение ошибки Java NullPointerException. Это практический проект. Кто-нибудь может объяснить причину исключения?

Nov 10, 2018 6:47:58 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C Exception in thread "main" java.lang.NullPointerException at 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();              
    }   
}

1 Ответ

0 голосов
/ 10 ноября 2018

Наиболее вероятная причина в том, что driver переопределяется в методе invokefirefoxflipkart, и, таким образом, ссылка на него в exitfirefoxflipkart() и searchlaptop() использует переменную экземпляра, но invokefirefoxflipkart используя местный.

Исправлено изменение строки в методе invokefirefoxflipkart() на:

driver = new FirefoxDriver();

Я добавил комментарии в отрывках кода.

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");
...