java语言NullPointerException

java语言NullPointerException,java,selenium-webdriver,nullpointerexception,pageobjects,findby,Java,Selenium Webdriver,Nullpointerexception,Pageobjects,Findby,使用SeleniumWedriver实用指南中的一个示例(登录并在Wordpress上创建测试帖子),我遇到了上述错误 以下是我正在使用的PageObject: package com.PageObjects; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.

使用SeleniumWedriver实用指南中的一个示例(登录并在Wordpress上创建测试帖子),我遇到了上述错误

以下是我正在使用的PageObject:

package com.PageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by JTester on 3/9/2016.
 */
public class AdminLoginPage {
    WebDriver driver;
    @FindBy(how=How.ID, id="user_login")
    WebElement email;
    @FindBy(how=How.ID, id="user_pass")
    WebElement pwd;
    @FindBy(how=How.ID, id="wp-submit")
    WebElement submit;

    public AdminLoginPage(WebDriver driver){
        this.driver = driver;
        driver.get("http://mysite.wordpress.com/wp-admin/");
        }

    public void login(){
        email.sendKeys("myEmailAddress@yahoo.com");
        pwd.sendKeys("myPasswd");
        submit.click();
    }

}
下面是测试类:

package com.features.trial;

import com.PageObjects.AdminLoginPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * Created by JTester on 3/9/2016.
 */
public class TestAddNewPost {
    public static void main(String... args) {
        WebDriver driver = new FirefoxDriver();
        //login to wordpress admin
        AdminLoginPage admLoginPage = new AdminLoginPage(driver);
        admLoginPage.login();

        //go to AllPosts page
        driver.get("http://mysite.wordpress.com/wp-admin/edit.php");

        //add a new post
        WebElement addNewPost = driver.findElement(By.linkText("Add New"));
        addNewPost.click();

        //add new post's content
        driver.switchTo().frame("content_ifr");
        WebElement postBody = driver.findElement(By.id("tinymce"));
        postBody.sendKeys("This is my description.");
        driver.switchTo().defaultContent();
        WebElement title = driver.findElement(By.id("title"));
        title.sendKeys("This is my first post");

        //publish my post
        WebElement publish = driver.findElement(By.id("publish"));
        publish.click();
        }
}
由于我不熟悉Selenium和Page对象,有谁能解释一下它为什么会抛出以下错误:

Exception in thread "main" java.lang.NullPointerException
    at com.PageObjects.AdminLoginPage.login(AdminLoginPage.java:29)
    at com.features.trial.TestAddNewPost.main(TestAddNewPost.java:17)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1
非常感谢

ps:在看了很多web上的代码示例之后,我认为我需要使用PageFactory来实例化我的元素,但不确定这是否正确,如果是,我如何使用示例代码来实现它

调试器屏幕截图:

应该使用PageFactory初始化AdminLoginPage

import org.openqa.selenium.support.PageFactory;

public AdminLoginPage(WebDriver driver){
    PageFactory.initElements(driver, this);  //add this
    this.driver = driver;
    driver.get("http://mysite.wordpress.com/wp-admin/");
}

添加以下代码:

AdminLoginPage loginPage = PageFactory.initElements(driver,AdminLoginPage.class);
        loginPage.login();
到TestAddNewPost类,而不是使用现有代码:

AdminLoginPage admLoginPage = new AdminLoginPage(driver);
        admLoginPage.login();

当您使用调试器并在
login
方法中检查
AdminLoginPage
的字段状态时,会发生什么情况?@Dai,好的,这是一个非常新的问题,您能帮我把它简化一下吗?如何检查字段状态?使用intelliJ。。在调试器中,我注意到它显示了变量,下面显示了以下内容:!框架不可用。@Dai,好的,在使用调试器单击TestAddNewPost类之后,我得到了一个变量列表?但我不确定这意味着什么。对此太陌生,无法理解:-(请参阅文章末尾的屏幕截图。已解决..我说我需要PageFactory来实例化我的元素是正确的..我已更改TestAddNewPost类以反映这一点..供将来参考,堆栈跟踪包含行号。粘贴的代码不包含行号。因此,阅读您的问题的人仍然不知道异常发生在哪一行来自。我将把如何帮助读者了解错误所在作为一项练习留给您。谢谢,这对我也很有用。我也发布了一个答案,但哪一个更好。我认为您的答案?最好从PageObject中实例化PageFactory,而不是TestAddNewPost类。。