RE: java.lang.NullPointerException

Answered

Newbie here and I am getting java.lang.NullPointerException for the simple test below, out of ideas now. I have shown the stack trace error below and also both of my pages and my test lastly. Any help or advice greatly appreciated!

java.lang.NullPointerException
at mytestpack.A_Login.test(A_Login.java:45)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

——————————————————————————–
package mypagespack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class A_LoginPage {

final WebDriver driver;

//Identify Username inputbox
@FindBy(id = “UserName”)
WebElement username;

//Identify Password inputbox
@FindBy(id = “Password”)
WebElement password;

//Identify logo – Potentially requires form after body
@FindBy(xpath = “/html/body/div[3]/div/img”)
WebElement logo;

//Identify Login button
@FindBy(id = “butLogin”)
WebElement loginbutton;

//Identify Lower logo
@FindBy(xpath = “/html/body/div[6]/div/div[1]/img”)
WebElement lowerlogo;

//Identify email address link
@FindBy(xpath = “/html/body/div[6]/div/div[2]/a”)
WebElement emaillink;

//Identify support text
@FindBy(xpath = “/html/body/div[6]/div/div[2]/text()[1]”)
WebElement loginsupport;

//Intialise elements
public A__LoginPage(WebDriver driver) {
this.driver = driver;
}

//Method to add username and password arguments
public void loginTo(String strusername, String strPassword){

//Fill user name
username.sendKeys(strusername);

//Fill password
password.sendKeys(strPassword);

//Click Login button
loginbutton.click();
}

}
——————————————————————————–-
package mypagespack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class B_HomePage {

final WebDriver driver;

//Identify Logout button
@FindBy (xpath = “/html/body/div/div[2]/div[1]/a/img”)
public WebElement logout;

//Identify the logged in user
@FindBy (xpath = “/html/body/div/div[2]/div[2]/span[1]”)
public WebElement loggedinuser;

//Initialise elements
public B_HomePage(WebDriver driver) {
this.driver = driver;
}

}
—————————————————————————————–
package mytestpack;

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.*;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import mypagespack.B_HomePage;
import mypagespack.A_LoginPage;

public class A_Login {
static WebDriver driver;
A_LoginPage LoginPage;
B_HomePage HomePage;

@BeforeMethod

public void beforeMethod(){
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“http://test.com”);

HomePage = PageFactory.initElements(driver, B_HomePage.class);

LoginPage = PageFactory.initElements(driver, A_LoginPage.class);
}

@Test
public void test(){

LoginPage.loginTo(“x”,”x”);

System.out.println(“Login Successfully Completed”);

HomePage.logout.click();
}

@AfterMethod

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

}

Add Comment
2 Answers

Check if this is causing problem name of the class is wrong

//Intialise elements
public A__LoginPage(WebDriver driver) {

as i can se there are A__ (underscore twice) @stevie24xx

Default Answered on May 7, 2016.

That must have been a typo here as it is a single underscore in my code, I edited the names slightly for privacy purposes – Thanks for the suggestion though 🙂

on May 9, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.