Getting PageFactory related error - java.lang.NullPointerException.

514 views
Skip to first unread message

bs

unread,
Feb 15, 2012, 12:52:07 AM2/15/12
to Selenium Users
Getting PageFactory related error - java.lang.NullPointerException.
I initialized 'Test1Class' in which I placed the FindBy annotation.
What is the error here.

---------------
public class Test1Class
{
@FindBy(name="q")
public WebElement searchBox;

@Test
public void CreateDriverAndSetup()
throws Exception
{
try{
WebDriver driver = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "");
driver.get("http://www.google.com");

//Initialization here...
PageFactory.initElements(driver, Test1Class.class);
searchBox.sendKeys("were");

} //method ends here

} // class ends here

bs

unread,
Feb 15, 2012, 2:57:09 AM2/15/12
to Selenium Users
I understood the issue. All the fields and methods should be in a
separate class or, even in the same class, the method should be a
different one.

This static method (PageFactory.initElements) always returns the
initialized class's object. With this object only we have to achieve
the task.

---- this code should be in another class ----------- E.g. TestClass2

temp1 tco= PageFactory.initElements(driver, temp1.class);
temp1.searchAll("test text");
------------------------------------------------


public class temp1 {
@FindBy(name="p")
public WebElement searchBox;

public void searchAll(String te)throws Exception
{
searchBox.sendKeys(te);

bs

unread,
Feb 15, 2012, 2:56:53 AM2/15/12
to Selenium Users
Reply all
Reply to author
Forward
0 new messages