NUll Pointer Exception using getTilte(); Selenium with Java

Viewed 19

Have two classes one Login and the other which is the TestNG class, I created variable to get the Title of the page but when trying to run the test it is displaying null pointer exception, if I remove the getTitle variable it runs.

package main;


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

public class Login {


WebDriver driver ;

By userNameTextBox = By.name("email");
By passwordTestBox = By.id("password");
By loginButton = By.id("signInButton");
String titleText = driver.getTitle();




public Login(WebDriver driver) {

    this.driver=driver;
}


public void setUserName() {

    driver.findElement(userNameTextBox).sendKeys("v-tobias.rivera@shutterfly.com");

}
public void setPassword() {

    driver.findElement(passwordTestBox).sendKeys("Indecomm1");

}
public void clickLogin() {

    driver.findElement(loginButton).click();;

}

}

package test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;
import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
//import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import main.Login;



public class LoginTest {

WebDriver driver;
Login objLogin;

@BeforeMethod
public void beforeTest() {
    System.setProperty("chromedriver", "/Users/tobiasriveramonge/eclipse- 
workspaceAutomation/seleniumAutomation");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://accounts.shutterfly.com/? 
    redirectUri=https%3A%2F%2Fwww.shutterfly.com%2F&cid=&brand=SFLY&theme=SFLY");
    WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
    WebElement element = 
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
    element.isDisplayed();
}

@Test
public void Test() {
    objLogin = new Login(driver);
    //String title = objLogin.getTitle();
   // System.out.println(title);
    //Assert.assertTrue(loginPageTitle.toLowerCase().contains(" "));
    objLogin.setUserName();
    objLogin.setPassword();
    objLogin.clickLogin();
}

@AfterTest
public void afterTest() {
    driver.quit();
}

}

I would like to know why am I getting this exception.

Hi, Have two classes one Login and the other which is the TestNG class, I created variable to get the Title of the page but when trying to run the test it is displaying null pointer exception, if I remove the getTitle variable it runs.

1 Answers

You did not initialize the driver object.
That's why referencing to that not initialized object throws the null pointer exception.
You only declared the driver object type by

WebDriver driver;

but never initialized it.

Related