0

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

I am trying to create sample framework but facing issue as url is not opening correctly and also browser is opening twice .

Chrome browser is opening twice and also we are seeing null value when debugging driver.

Below is the step definition file:

public class steps  {
    
    private WebDriver driver;

    Baseclass base = new Baseclass(driver);
    Loginpage login = new Loginpage(driver);

    public static Logger logfile = LogManager.getLogger(steps.class.getName());
    
    @Given("open chrome browser")
    public void openBrowser() {
        base.intiatingDriver();
        
    }
    @And("Login to application url")
    public void loginAppicaltionUrl()  {
        login.openUrlLogin("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
        
    }
    @And("^enter the valid \"(.*)\"$")
    public void enterValidUsername(String username) {
        login.enteringUsername(username);
    }
    
    @And("^enter valid \"(.*)\"$")
    public void enterValidPassword(String password) {
        login.enteringPassword(password);   
    }

Baseclass:

public class Baseclass {
    
    private  WebDriver driver=null;
    
    public static Logger logfile = LogManager.getLogger(Baseclass.class.getName());

    public static String parentWindow;

    
    public Baseclass(WebDriver driver) {
        
        this.driver = driver;
        PageFactory.initElements(driver, this);
        }
    
    public void intiatingDriver() {
        
        driver =new ChromeDriver();
        driver.manage().window().maximize();

    }

    public void openUrl() throws Throwable{
        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
        threadsleep(5000);
        logfile.info("Url opened successfully");
    }
    
    public void sendKeysUsingCss(WebElement element,String username) {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(40));
        element.sendKeys(username);
        
    }

Login class:

public class Loginpage  {                                                     
                                                                                                                                                                                                                      
    private  WebDriver driver=null;                                           
                                                                                                                                                   
    public Loginpage(WebDriver driver) {                                      
        this.driver = driver;                                                 
        PageFactory.initElements(driver, this);                               
                                                                              
    }                                                                         
                                                                              
    Baseclass base = new Baseclass(driver);                                   
                                                                              
                                                                              
    @FindBy(how=How.CSS, using="input[name^='username']")                     
    private WebElement name;                                                  
                                                                              
    @FindBy(how=How.CSS,using="input[name^='password']")                      
    private WebElement passwrd;                                                                                                                      
                                                                              
    public void openUrlLogin(String url) {                                    
        try {                                                                 
            driver.get(url);                                                  
            driver.manage().window().maximize();                              
            base.threadsleep(5000);                                           
            base.logfile.info("Url opened successfully");                     
        } catch (Throwable e) {                                               
            e.printStackTrace();                                              
            System.out.println(e);                                            
            Assert.fail("Url not opened");                                    
                                                                              
        }                                                                     
    }                                                                         
                                                                              
    public void enteringUsername(String username) {                           
        name.sendKeys(username);                                              
                                                                              
    }   

1 Answer 1

0

You are getting NullPointerException, because you haven't inited any driver instance.

In BaseClass and LoginClass you pass driver in constructor, then call PageFactory.initElements(driver, this);. initElements method expects that driver has already exist as instance, but at that moment it's null.

This lines are called in runtime on class creation. In this moment you don't have any instance of driver, so you get the exception.

   private WebDriver driver;

   Baseclass base = new Baseclass(driver);
   Loginpage login = new Loginpage(driver);

To solve this you should create a method that inits your driver and is called before class creation.

    private WebDriver driver;

    @BeforeClass
    public static void setupAll() {
        WebDriverManager.getInstance().setup();
        driver = new ChromeDriver();
    }

Where WebDriverManager defined as a depenedency in pom.xml

      <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.3.2</version>
      </dependency>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks , i have added the chrome driver initiation but in login page url method is getting null in driver public void intiatingDriver() { System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver.exe"); driver =new ChromeDriver(); driver.manage().window().maximize(); }
You don't need this method. You can just init your browser like I suggested, in test or base test method, using @BeforeClass annotation.
Ya i did it in baseclass still not able to open the application url
In your implementation you need to do it in public class steps.

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.