Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null

Viewed 15160

when I try to run below code in testNg null pointer exception shown in Eclipse

public class ImgDDChkbxRadio  {
    WebDriver driver;
    
    @BeforeTest
    public void LaunchBrowser()
    {
        System.setProperty("webdriver.chrome.driver","F:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://www.leafground.com/");
    }   
    @Test
    public void Img()
    {   
        driver.findElement(By.xpath("//img[@src='images/image.png']")).click();
        driver.findElement(By.xpath("//*[@src=\"../images/home.png\"]")).click();
        driver.navigate().back();
        driver.findElement(By.xpath("//*[@src=\"../images/abcd.jpg\"]")).click();
        
    }
}
8 Answers

Just remove the WebDriver from LaunchBrowser() and I hope this will work for you ;)

    public class ImgDDChkbxRadio  {
    WebDriver driver;
    
    @BeforeTest
    public void LaunchBrowser()
    {
        
    System.setProperty("webdriver.chrome.driver","/Users/chrome/chromedriver");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://www.leafground.com/");
    }   
    @Test
    public void Img()
    {   
        driver.findElement(By.xpath("//img[@src='images/image.png']")).click();
        driver.findElement(By.xpath("//*[@src=\"../images/home.png\"]")).click();
        driver.navigate().back();
        driver.findElement(By.xpath("//*[@src=\"../images/abcd.jpg\"]")).click();
        
    }
}

I've learned since I've made this comment.

My new suggestion is - delete all imports at the beginning of your file and add them again. Be cautious about proper imports - especially versions of your test suite - issue can occur when code uses few different versions.

How to add imports? Move your cursour over marked classes, methods, etc. and react accordingly to your IDE suggestions (Alt + Enter for IntelliJ for Windows).

OLD and probably unsafe workaround:

My old suggestion can be a workaround for your issue but could make a lot of new instances and I guess it's not a wise thing to do. Tests will work but more like a by-product.

Create an instance of ChromeDriver before using it in your LaunchBrowser() method:

public class ImgDDChkbxRadio  {
    WebDriver driver = new ChromeDriver();
    
    @BeforeTest
    public void LaunchBrowser()
    {
        System.setProperty("webdriver.chrome.driver","F:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://www.leafground.com/");
    }   
    @Test
    public void Img()
    {   
        driver.findElement(By.xpath("//img[@src='images/image.png']")).click();
        driver.findElement(By.xpath("//*[@src=\"../images/home.png\"]")).click();
        driver.navigate().back();
        driver.findElement(By.xpath("//*[@src=\"../images/abcd.jpg\"]")).click();
        
    }
}

I think it may be the result of some changes made in JUnit 5 in annotations (@).

It could be the the site is not being loaded - are you able to launch the site manually?

If yes try this:

  • Make WebDriver object static (write static before the variable name)
  • Try to add Thread.Sleep(5000) to LaunchBrowser method, after you open the site using the browser

You are creating an object of the Browser with the help of the WebDriver Interface.

WebDriver{ driver = new new EdgeDriver();} but the scope of this driver is within method only. To use it, you declared it publicly.

The lifetime of the driver is only in the WebDriver interface. Making it: driver=new EdgeDriver();

can only solve your issues: java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null

package ADUMMYExersice;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class stackoverflowNull {
    WebDriver driver;
        
        @BeforeTest
        public void LaunchBrowser()
        {
            System.setProperty("webdriver.edge.driver", "C:\\Selenium Files\\BrowserExe\\msedgedriver.exe");
             
            driver=new EdgeDriver();
            driver.manage().window().maximize();
            driver.get("http://www.leafground.com/");
        }   
        @Test
        public void Img()
        {   
            driver.findElement(By.xpath("//img[@src='images/image.png']")).click();
            driver.findElement(By.xpath("//*[@src=\"../images/home.png\"]")).click();
            driver.navigate().back();
            driver.findElement(By.xpath("//*[@src=\"../images/abcd.jpg\"]")).click();
               }
    }

It could be an issue with your Chrome browser too - try it with Firefox and still does not work then try to change your workspace. Had the same issue wasted 6hrs trying and searching. worked for me with Firefox.

protected static WebDriver driver; use above and check if you have used web driver in any other class ?

I had two classes and in that I gave a variable name as public WebDriver driver; but it did not work it throwed me a error message saying

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null.

After that I found an answer which we need to give static before the variable public static WebDriver driver;

We I have create a test script and using testing I have facing center code here` that kind of problem Please Check my code and resolved my Prblem package testcase;

import org.openqa.selenium.By; import org.testng.annotations.Test;

import base.Basetest;

public class MyFirstTestFW extends Basetest{

@Test

public static void LoginTest() throws InterruptedException
{
    


System.out.println("Clicked Successfully");
driver.findElement(By.linkText("Sign in")).click(); //locals----properties
driver.findElement(By.id("login_id")).sendKeys("vaishali.verma@techinfini.in");
driver.findElement(By.xpath("//span[normalize-space()='Next']")).click();
Thread .sleep(4000);
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("Vaishu@123");
Thread .sleep(4000);
driver.findElement(By.xpath("//button[@id='nextbtn']//span[contains(text(),'Sign in')]")).click();
Thread .sleep(4000);


}

}

Related