How to solve Null Pointer Exception for below Selenium WebDriver POM Project



  • TestBase.java

    public class TestBase {
    
        public static WebDriver driver;
        public static Properties prop;
    
    
        public TestBase() {
    
        public static void intialization() {
            String browserName = prop.getProperty("browser");
            if (browserName.equals("chrome")) {
                System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
                driver = new ChromeDriver();
            }
            else if (browserName.equalsIgnoreCase("FireFox")) {
                System.setProperty("webdriver.gecko.driver", "E:\\Selenium\\geckodriver.exe");
                driver = new FirefoxDriver();
            }
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
            driver.get(prop.getProperty("url"));
        }
    
    }
    ```
    
    
    

    LoginPage.java

    public class LoginPage extends TestBase{
    
        @FindBy(xpath="//input[@name='email']")
        WebElement emailField;
    
        @FindBy(xpath="//input[@name='password']")
        WebElement passwordField;
    
        @FindBy(xpath="//div[@class='ui fluid large blue submit button']")
        WebElement loginBtn;
    
        @FindBy(xpath="//a[contains(text(),'Forgot')]")
        WebElement forgotPass;
    
        @FindBy(xpath="//a[contains(text(),'Classic CRM')]")
        WebElement classicCRM;
    
        @FindBy(xpath="//a[contains(text(),'Sign')]")
        WebElement signUp;
    
        public String getLoginPageTitle() {
            return driver.getTitle();
        }
    
        public boolean loginButtonEnabled() {
            return loginBtn.isEnabled();
        }
    
        public ForgotPassPage forgotPassEnabled() {
            forgotPass.click();
            return new ForgotPassPage();
        }
    
        public boolean classicCRMEnabled() {
            return classicCRM.isDisplayed();
        }
    
        public ClassicCRMPage classicCRMRedirect() {
            classicCRM.click();
            return new ClassicCRMPage();
        }
    
        public boolean signUpEnabled() {
            return signUp.isDisplayed();
        }
    
        public HomePage login(String user, String pass) {
            emailField.sendKeys(user);
            passwordField.sendKeys(pass);
            loginBtn.click();
            return new HomePage();
        }
    
    }
    ```
    
    
    

    LoginPageTest.java

    public class LoginPageTest extends TestBase {
        LoginPage loginPage;
        FrontPage frontPage;
        ForgotPassPage forgetPassPage;
        ClassicCRMPage classicrmPage;
    
        public LoginPageTest() {
            super();
        }
        @BeforeMethod
        public void setUp() {
            intialization();
            frontPage = new FrontPage();
            frontPage.loginRedirect();
            loginPage = new LoginPage();
        }
    
        @Test(priority=1)
        public void testLoginPageTitle() {
            String title = loginPage.getLoginPageTitle();
            Assert.assertEquals(title, "Cogmento CRM");
        }
    
        @Test(priority=2)
        public void testLoginButtonEnable() {
            boolean btn = loginPage.loginButtonEnabled();
            Assert.assertTrue(btn);
        }
    
        @Test(priority=3)
        public void testClassicCRMDisplay() {
            Assert.assertTrue(loginPage.classicCRMEnabled());
        }
    
        @Test(priority=4)
        public void testClassicCRMRedirect() {
            classicrmPage = loginPage.classicCRMRedirect();
        }
    
        @Test(priority=5)
        public void testForgetPassword() {
            forgetPassPage = loginPage.forgotPassEnabled();
        }
    
        @AfterMethod
        public void tearDown() {
            driver.quit();
        }
    
    }   
    ```
    
    
    

    When I try to run Test File using TestNG I am getting below error message: java.lang.NullPointerException

    com.crm.qa.pages.LoginPage.loginButtonEnabled(LoginPage.java:39) com.crm.qa.tests.LoginPageTest.testLoginButtonEnable(LoginPageTest.java:39)

    I am learning Selenium WebDriver and this is for my personal experience. Not related to any working Project

    Any help will be appreciated.



  • As it turned out, I haven't initialized my Page Object Elements. After Initializing it worked as Charm


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2