0

I have the following error and I would appreciate if someone can tell me what am I doing wrong. I'm trying to run test in navigateAllMenus.java but the moment the script tries to locate loginLink, it stops and gives me java.lang.NullPointerException in line 28: _a_LoginPage.loginLink(driver).click();.

I have tried many times with different xpaths for the same element but nothing works. I am at a Jr level and still get confused with certain things. So here's what I have, thank you in advance.

Trace:

java.lang.NullPointerException
at autFwk.navigateAllMenus.test(navigateAllMenus.java:28)
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:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Test case:

package autFwk;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pagObj._b_HomePage;
import pagObj._a_LoginPage;

public class navigateAllMenus {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver","ChromeDriver/chromedriver.exe");
    }

    @Test
    public void test() {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.interaction-design.org/");
        _a_LoginPage.loginLink(driver).click();
        _a_LoginPage.email(driver).sendKeys("[email protected]");
        _a_LoginPage.password(driver).sendKeys("password");
        _a_LoginPage.loginButton(driver).click();
        _b_HomePage.Profile(driver).click();
        driver.navigate().back();
        _b_HomePage.Courses(driver).click();
        driver.navigate().back();
        _b_HomePage.Community(driver).click();
        driver.navigate().back();
        _b_HomePage.Logout(driver).click();
        assertTrue(_a_LoginPage.loginLink(driver).isDisplayed());
        driver.quit();
    }

}

Object loginLink is the one that seems to be giving me trouble:

package pagObj;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class _a_LoginPage {

    private static WebElement element = null;

    public static WebElement loginLink (WebDriver driver){
        driver.findElement(By.xpath("//a[@href='https://www.interaction-design.org/login']"));
        return element;
1
  • 1
    Please accept the answer posted by DebanjanB as the accepted answer. Commented Nov 7, 2017 at 12:52

1 Answer 1

1

You are getting a NullPointerException because in the _a_LoginPage Class you haven't initialized the WebDriver instance i.e. driver and haven't written the constructor. So you have to add the following in the _a_LoginPage Class :

//initialize the WebDriver instance
WebDriver driver;

//constructor
public _a_LoginPage(WebDriver driver)
{
    this.driver=driver;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi DebanjanB, I tried your suggestion but I'm getting the same error... the WebDriver instance and the constructor go inside the Class, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.