New to automation. Help is greatly needed.
Why am I getting a NullPointerException
?
Error: Null pointer access: The variable driver can only be null at this
location
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import common.controls.PageVerified;
public class Google extends PageVerified{
PageVerified PgVerif = new PageVerified();
WebDriver driver = new ChromeDriver();
@Test
public void Login() throws Exception{
driver.manage().window().maximize();
driver.get("https:Google.com");
driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test");
Thread.sleep(2000);
driver.findElement(By.className("lsb")).click();
PgVerif.Verify();
String a = driver.getTitle();
System.out.println(a);
driver.findElement(By.xpath("//*[@id='lst-ib']")).clear();
driver.findElement(By.xpath("//*[@id='lst-ib']")).sendKeys("Test");
Thread.sleep(2000);
driver.findElement(By.className("sbico-c")).click();
String b = driver.getTitle();
System.out.println(b);
driver.close();
}
Following is another class that I am calling where I get the error and it is in separate package than the above.
package common.controls;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class PageVerified
{
public String Verify()
{
WebDriver driver = null ;
String pagetitle = driver.findElement(By.xpath("//*[@id='gb_70']")).getText();
if(pagetitle != null)
{
System.out.println(pagetitle + "Verified Successfully" );
return pagetitle;
}
else
{
System.out.println("Error::Not Verified");
}
return null;
}
}