Reputation: 1
I am getting a NullPointerException when I execute the below code. Please help me with the solution. I have done using page object model. unable to perform the further actions.
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();
}
}
Exception
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)
Upvotes: 0
Views: 2739
Reputation: 342
Ok so you have passed the driver to the loginobjects before initilizing it so you are gettting the error.
Edit logintestcase class as below by putting initilization before the passing of driver.
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();
}
}
Upvotes: 1