Quality Testing

Quality is delighting customers

How to solved Java null pointer error java.lang.NullPointerException in Selenium webdriver using Test NG in eclipse ? simple way :)

How to solved Java null pointer error java.lang.NullPointerException in Selenium webdriver using Test NG in eclipse ? simple way :)

I have written the following code in webdriver using TestNG frame work but when

I was running this below code,

it was giving me Java null pointer exception error –

FAILED CONFIGURATION: @AfterTest close

java.lang.NullPointerException

at Default.Smoke.close(Smoke.java:41)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

package Default;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.AfterTest;

public class Smoke {

public String baseUrl = “http://website”;

public WebDriver driver;

@BeforeTest

public void beforeTest() {

// Login in the application

System.setProperty(“webdriver.chrome.driver”, “path for chrome driver”;

WebDriver driver = new ChromeDriver();

driver.get(baseUrl);

driver.findElement(By.name(“txtUserid”)).sendKeys(“name”);

driver.findElement(By.name(“txtPassword”)).sendKeys(“pass”);

driver.findElement(By.name(“btnLogin”)).click();

driver.manage().window().maximize();

}

@Test

public void asset() {

driver.get(“page url”);

}

@AfterTest

public void close() {

driver.quit();

}

}

So I have got the solution and I am sharing with you all –

You just have to check this line in your above code

System.setProperty(“webdriver.chrome.driver”, “path for chrome driver”;

WebDriver driver = new ChromeDriver();

driver.get(baseUrl);

As you can see in my above code that I have already defined the public variable

“public String baseUrl = “http://website”;

public WebDriver driver;

in my starting of the class, so next time webdriver gets confused when I write webdriver in front of driver in my @before test class , here :

@BeforeTest

public void beforeTest() {

// Login in the application

System.setProperty(“webdriver.chrome.driver”, “path for chrome driver”;

WebDriver driver = new ChromeDriver();

So the solution is just remove this “webdriver” word in your beforetest

class and your code will be like –

@BeforeTest

public void beforeTest() {

// Login in the application

System.setProperty(“webdriver.chrome.driver”, “path for chrome driver”;

driver = new ChromeDriver();

Now run the whole code and it will show you the success which you are wanting to get :)

Views: 3361

Reply to This

Replies to This Discussion

Selenium Webdriver Tutorial for Beginners |Selenium Webdriver Training Videos

Thank you for the sollution.

Welcome... you can get more videos regarding testing & Katalon too on my below channel

https://youtu.be/ZjWitjqkKJI

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2021   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service