-2

I have two classes a Base class and Test class

Base class:

public class Base {
    
    public static WebDriver driver;

    public void browser() {

    System.setProperty("webdriver.chrome.driver", 
     "C:\\Users\\admin\\Desktop\\Selenium\\chromedriver.exe");

    driver  = new ChromeDriver();
    
    driver.manage().window().maximize();
    
    }

And, I am extending my Test classes to the Baseclass to use the driver reference,

public class FacebookTestCase extends Base{
    
    
    public static void main(String[] args ) {
            
        driver.get("www.facebook.com"); 

        driver.findElement(By.id("email")).sendKeys("tetst@test.in");

        driver.findElement(By.id("pass")).sendKeys("test");
        driver.findElement(By.name("login")).click();
        
    }

}

But I am getting Null pointer exception when I run the Test class.

Exception in thread "main" java.lang.NullPointerException at grid.practice.FacebookTestCase.main(FacebookTestCase.java:12)

EDITED

I have another query,

Now I am separating my Test class and Page class and I am calling my page class methods from my test class. I am creating an object of the page class and calling the methods. But I get null pointer exception in the Page class when I pass the driver to the constructor of the page class.

Test class:

public class FacebookTestCase extends Base{
    
    
    public static void main(String[] args ) {
            
    driverInitialization();
    FacebookElements fb = new FacebookElements(driver);
    fb.login();
        
        
    }

Page class:

public class FacebookElements{

    WebDriver driver;

    public FacebookElements(WebDriver driver) {
    this.driver = driver;

    }
    
    
    public void login() {

    driver.findElement(By.id("username")).sendKeys("test");
    driver.findElement(By.id("password")).sendKeys("test");
    }

}

2 Answers2

0

Cause you haven't called that browser method in your main method. call that

public static void main(String[] args ) {
    browser():
    driver.get("www.facebook.com"); 
// Then rest of your code. 
}

Also since main is a static method, you simply won't be able to call browser method inside it. So either make browser as static or create object of base class and use the browser method.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

You did not initialize the driver object.
The driver initialization is performed by browser method in Base class.
You never called it in runnable main method inside the FacebookTestCase class.

Prophet
  • 32,350
  • 22
  • 54
  • 79