2

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();
        
    }
}
4
  • Format your question Commented Apr 18, 2021 at 6:16
  • Add more data on the question or remove some of your code Commented Apr 18, 2021 at 6:26
  • On what line of the code do you receive the null pointer exception? Commented Apr 18, 2021 at 7:12
  • In your Test, the driver refers to the field in the class ImgDDChkbxRadio. However, you have initialized a local variable with the same name in BeforeTest. Inside the BeforeTest, the local variable driver hides the global variable and hence is never initialized. Instead of WebDriver driver=new ChromeDriver();, try this.driver = new ChromeDriver(). Commented Apr 18, 2021 at 7:59

9 Answers 9

1

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();
        
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 (@).

Comments

0

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

Comments

0

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

Comments

0

I had simular problem. I changed "WebDriver driver" to "public static Webdriver".

This worked by me.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

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.

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question.
-1

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

2 Comments

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.
You have posted three low quality answers under this question. If you think they are valuable, you should merge them and remove the other two. The question mark in the answers can seem as you are asking, please avoid it as well.
-1

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;

Comments

-1

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("[email protected]");
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);


}

}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.