java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null

3,621 views
Skip to first unread message

OBA

unread,
Jun 4, 2023, 12:29:34 AM6/4/23
to Selenium Users
I am getting the above titled error message, please help!


package com.tutorialsninja.qa.testcases;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.tutorialsninja.qa.base.baseClass;

public class Search extends baseClass {

public Search () {
super();
}

WebDriver driver;

@BeforeMethod
public void setup () {
driver = initializeBrowserAndOpenAppURL(prop.getProperty("browserName"));
}

@AfterMethod
public void tearDown () {
driver.quit();
}

@Test (priority=1)
public void verifySearchWithValidProduct () {
driver.findElement(By.name("search")).sendKeys("HP");
driver.findElement(By.xpath("//*[@id=\"search\"]/span/button/i")).click();
Assert.assertTrue(driver.findElement(By.linkText("HP LP3065")).isDisplayed());
}

@Test (priority=2)
public void verifySearchWithInvalidProduct () {
driver.findElement(By.name("search")).sendKeys("Lenovo");
driver.findElement(By.xpath("//*[@id=\"search\"]/span/button/i")).click();
String productWarning = driver.findElement(By.xpath("//p[contains(text(),'There is no product that matches the search criteria')]")).getText();
Assert.assertEquals(productWarning, "There is no product that matches the search criteria.", "No product warning about product search");
}

@Test (priority=3)
public void verifySearchWithoutAnyProduct () {
driver.findElement(By.name("search")).sendKeys("");
driver.findElement(By.xpath("//*[@id=\"search\"]/span/button/i")).click();
String productWarning = driver.findElement(By.xpath("//p[contains(text(),'There is no product that matches the search criteria')]")).getText();
Assert.assertEquals(productWarning, "There is no product that matches the search criteria.", "No product warning about product search");

}
}

Arvind Sah

unread,
Jun 4, 2023, 5:41:21 AM6/4/23
to seleniu...@googlegroups.com
Hi,

Something wrong in you beforeMethod.

Might be prop.getproperty isn't returning browser name or this  "initializeBrowserAndOpenAppURL"  method isn't initialising the driver instance.

Try running in debug mode, you will find the root cause 
Regards,
Arvind 


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/2cecb320-0431-44c6-a9bc-dbd63efb48d1n%40googlegroups.com.

mohanap...@gmail.com

unread,
Jun 4, 2023, 9:51:37 AM6/4/23
to Selenium Users
package com.tutorialsninja.qa.testcases;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Search {


WebDriver driver;

@BeforeMethod
public void setup() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
driver.get("your_app_url");

}

@AfterMethod
public void tearDown() {
driver.quit();
}

@Test(priority = 1)
public void verifySearchWithValidProduct() {

driver.findElement(By.name("search")).sendKeys("HP");
driver.findElement(By.xpath("//*[@id='search']/span/button/i")).click();
Assert.assertTrue(driver.findElement(By.linkText("HP LP3065")).isDisplayed());
}

@Test(priority = 2)
public void verifySearchWithInvalidProduct() {
driver.findElement(By.name("search")).sendKeys("Lenovo");
driver.findElement(By.xpath("//*[@id='search']/span/button/i")).click();
String productWarning = driver.findElement(By.xpath("//p[contains(text(),'There is no product that matches the search criteria')]")).getText();
Assert.assertEquals(productWarning, "There is no product that matches the search criteria.", "No product warning about product search");
}

@Test(priority = 3)
public void verifySearchWithoutAnyProduct() {
driver.findElement(By.name("search")).sendKeys("");
driver.findElement(By.xpath("//*[@id='search']/span/button/i")).click();
String productWarning = driver.findElement(By.xpath("//p[contains(text(),'There is no product that matches the search criteria')]")).getText();
Assert.assertEquals(productWarning, "There is no product that matches the search criteria.", "No product warning about product search");
}
}
  try  with  this  code

Sapna Chouhan

unread,
Jun 6, 2023, 4:01:05 AM6/6/23
to seleniu...@googlegroups.com
Because you are initializing webDriver but not create object for WebDriver
maybe this will help you
WebDriver driver = new WebDriver();

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages