0

I have Test base class which calls a method from LandingPage class which has the locator for the page under test. When I execute this project, I'm getting Null pointer exception. I'm sure it has something to do with testNG annotations, but I'm unable to find out the reason.

package com.xyz.tests; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.xyz.pageObjects.LandingPage; import com.xyz.pageObjects.LoginPage; import com.xyz.utils.Utils; import engine.Engine; public class LandingPageTest { public WebDriver driver; //Engine engine = new Engine(driver); LandingPage landingPage; LoginPage loginPage; @BeforeTest public void setUp(){ driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.go.com"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test public void clickOnSignInLink() { landingPage.SignIn().click(); } 

Below is my LandingPage class

 package com.xyz.pageObjects; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class LandingPage { private WebDriver driver; public LandingPage(WebDriver driver) { this.driver = driver; } //By signIn= By.xpath(".//*[@id='pageContainerInner']/div[2]/div[1]/div/div[2]/div[1]/a"); By signInBtn = By.linkText("Sign In or Create Account"); //By signInBtn2 = By.cssSelector("css=a.signIn"); By closeCrisisMessage = By.xpath(".//*[@id='closeCrisisMessageBtn']"); public WebElement SignIn(){ return driver.findElement(signInBtn); } } 

Following is the error message

 C:\Users\ad\AppData\Local\Temp\testng-eclipse--957796922\testng-customsuite.xml FAILED: clickOnSignInLink java.lang.NullPointerException at com.disney.tests.LandingPageTest.clickOnSignInLink(LandingPageTest.java:36) 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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 

Any suggestions will be appreciated.

1
  • Where is SignIn() Method ?CommentedOct 6, 2016 at 12:14

2 Answers 2

2

I'm not sure on what line your NullPointerException is thrown, but I would expect this to fail because your landingPage has not been initialized when the @Test method is run:

landingPage = new LandingPage(driver); 
1
  • Thanks a bunch :) I thought I had initialized it in a different class.
    – AdiBoy
    CommentedOct 6, 2016 at 12:16
1

I see, you didn't instantiate landingPage & loginPage anywhere. That's why you are getting a null pointer exception. To get around this issue, you have to instantiate those references (mainly, landingPage here). Instantiate it either in place of declaration or in @BeforeClass annotation.

1
  • Thanks, that helped :)
    – AdiBoy
    CommentedOct 6, 2016 at 12:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.