org.openqa.selenium.support.pagefactory.findElement(DefaultElementLocator.java:69)上的java.lang.NullPointerException

时间:2018-08-30 18:16:17

标签: java selenium webdriver

当我执行以下代码时,我得到了NullPointerException。 请帮我解决。我已经完成了使用页面对象模型的工作。 无法执行进一步的操作。

browser.java

public class browser {

    public WebDriver driver= null;

    public void initialize() {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\Teddy\\Downloads\\geckodriver-v0.21.0-win64\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        System.out.println("Application title is ============="+driver.getTitle());
    }

    public void window_close() {
        driver.quit();
    }
}

loginobjects.java

public class loginobjects extends browser {

    public loginobjects(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    @FindBy(id="lst-ib")
    public WebElement name;

    @FindBy(name="btnK")
    public  WebElement name2;

    public void login(){
        name.sendKeys("Selenium with Java");
        name2.click();
    }
}

logintestcase

public class logintestcase extends browser {
    public loginobjects log;

    @Test
    public void details() {  
        log= new loginobjects(driver);
        super.initialize();
        System.out.println("here");
        log.login();
        System.out.println("here");
        super.window_close();
    }
}

例外

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.click(Unknown Source)
    at loginobjects.login(loginobjects.java:28)
    at logintestcase.details(logintestcase.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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)

1 个答案:

答案 0 :(得分:1)

好的,因此在初始化驱动程序之前已将驱动程序传递给loginobject,这样您就可以得到错误。

通过在驱动程序通过之前进行初始化来编辑logintestcase类,如下所示。

public class logintestcase extends browser {
    public loginobjects log;

    @Test
        public void details()

        {
            super.initialize();
            log= new loginobjects(driver);
            System.out.println("here");
            log.login();
            System.out.println("here");
            super.window_close();


        }


    }