Наиболее вероятная причина в том, что 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");