0

LoginPage.Java

package TestCases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

import ObjectRepository.EnterCredentialsRepository;
import ObjectRepository.LaunchURLRepository;

public class LoginPage {

    WebDriver driver;
    LaunchURLRepository lpR;
    //EnterCredentialsRepository ecR;

    @Test
    public void LoginPageApp () throws Exception
    {

            lpR = new LaunchURLRepository();
            lpR.LaunchDriver();
            lpR.getURL();

            //ecR = new EnterCredentialsRepository();
            lpR.Username.sendKeys("admin");
            lpR.Password.sendKeys("admin");

    }


}

LaunchURLRepository.java

package ObjectRepository;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LaunchURLRepository {

    public static WebDriver driver;
    WebDriverWait wait;
    public static String url = "https://www.google.com";

    @FindBy(id="txtUserID")
    public WebElement Username;

    @FindBy(css="input#txtPassword")
    public WebElement Password;


    public WebDriver LaunchDriver()
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();

        wait = new WebDriverWait(driver,10);

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }

    public void getURL()
    {
        driver.get(url);
        driver.manage().window().maximize();
    }

    public LaunchURLRepository()
    {
        PageFactory.initElements(driver, this);

    }

}

I am getting below NullPointer execption.... Please Help

FAILED: LoginPageApp
java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy7.sendKeys(Unknown Source)
    at TestCases.LoginPage.LoginPageApp(LoginPage.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
1

Selenium will instantiate a WebElement object when you call PageFactory.initElements(driver, LaunchURLRepository.class)

Before this statement, any interaction with the WebElement attributes will result in a NullPointerException.

More details on the Selenium Wiki and the Javadoc of this method here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.