0

I am getting the NullPointerException after executing the below testng test script. After launching the URL when it comes inside the test script method then, it is throwing the exception. Can you please help me out in this.

(Object Repo) LakesAndMountainsHomePage.java :-

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.actitime.genericlib.WebDriverCommonLib;

public class LakesAndMountainsHomePage extends WebDriverCommonLib{

WebDriver driver;

public LakesAndMountainsHomePage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}


@FindBy(xpath= "//*[@id='the-main-menu']//*[contains(text() , 'LAKES & 
MOUNTAINS')]")
WebElement LandM;

public WebElement LandMHeader()
{
System.out.println("came inside the method");
//LandM.isDisplayed();
return LandM;
}

}

BaseTest.java:-

public abstract class BaseTest {

    ExcelLib eLib;
    WebDriverCommonLib wLib;
    WebDriver driver;
    HomePage homepage;

  @BeforeClass
  public void baseBeforeClass() 
  {
    eLib = new ExcelLib();
    wLib = new WebDriverCommonLib();
    driver=Driver.getBrowser();
    driver.manage().window().maximize();
    System.out.println("Browser is launched");
  }

  @BeforeMethod
  public void launchURL()
  {  
      wLib.homePage();
      //loginPage.loginToAPP();
      System.out.println("Navigated to the URL");
  }      
}

Testscript:-

public class LakesAndMountainsHomePageTest extends BaseTest{

LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver);

 //TC TC131409 [New Lakes & Mountains Tab] : Verify New Lakes & Mountains 
Tab is displayed in header. 
 @Test(priority=0)
  public void lakesAndMountainsHeader()
  {
boolean a= lm.LandMHeader().isDisplayed(); //getting exception here
if(a==true)
{
System.out.println("Lakes And Mountains tab is present in the HomePage");
}
else
{
System.out.println("Lakes And Mountains tab is not present in the 
HomePage");
}
      lm.LandMHeader().click(); 
      System.out.println("It has clicked the tab");
  }
}

And below is the exception stack trace:-

below is the exception stacktrace:-

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.$Proxy9.isDisplayed(Unknown Source) at com.acttime.usertest.LakesAndMountainsHomePageTest.lakesAndMountainsHeader(LakesAndMountainsHomePageTest.java:27) 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:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:669) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201) 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:776) at org.testng.TestRunner.run(TestRunner.java:634) at org.testng.SuiteRunner.runTest(SuiteRunner.java:425) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385) at org.testng.SuiteRunner.run(SuiteRunner.java:334) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318) at org.testng.TestNG.runSuitesLocally(TestNG.java:1243) at org.testng.TestNG.runSuites(TestNG.java:1161) at org.testng.TestNG.run(TestNG.java:1129) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

I am getting the NullPointerException after executing the below testng test script. After launching the URL when it comes inside the test script method then, it is throwing the exception. Can you please help me out in this.

3
  • Possible duplicate of NullPointerException in my code. How to deal with it Commented Nov 28, 2017 at 8:19
  • the other query is having different issue, as in my code i am able to launch the URL in browser, but when i am doing some actions from my object repository using page factory, i am getting NullPointerException. Commented Nov 28, 2017 at 8:35
  • Its a good practice to attach exception stacktrace @shashanksinha ! Commented Nov 28, 2017 at 10:01

2 Answers 2

1

This instantiation is illegal.

LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver);

It is the root cause of Null pointer exception.

Move this statement to BaseTest class, create a class field and instantiate inside @BeforeClass method . Also, remove this operator.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @Manmohan_singh. the code is executing now.
Can you please explain what is the issue if i am keeping LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver); statement in my test script class?
The test script class is inheriting TestNG annotated methods from BastTest , Since no constructors are defined , the instantiation of LakesAndMountainsHomePage is never picked up by JVM . To bring this into the execution flow, you have to move your instantiation into @BeforeClass method. Alternatively, you can make use of class constructors also.
0

Well i'am unable to see the implementation of a driver class specifying the type of driver(firefox,chrome,IE) being initialized.

1 Comment

that is initialized in a different Driver java class.

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.