why getting NullPOinterException when page object is declared at instance level

why getting NullPOinterException when page object is declared at instance level

Questions : why getting NullPOinterException when page object is declared at instance level

588
package Repaired_IETestCases; import org.openqa.selenium.By; import org.openqa.selenium.support.PageFactory; import org.testng.annotations.Test; import cucumber.TestContext; import managers.WebDriverManager; import pageObjects.CartPage; public class CartPageSteps extends WebDriverManager { @Test public void verifyValLogo() throws Exception { CartPage cartPage = PageFactory.initElements(driver, CartPage.class); cartPage.verifyLogoPresentOnHomePage(); } @Test public void verifyHomePage() throws Exception { CartPage cartPage = PageFactory.initElements(driver, CartPage.class); cartPage.verifyHomePage(); } } 

If I declare “CartPage cartPage = in4codes_selenium-webdriver PageFactory.initElements(driver, in4codes_selenium-webdriver CartPage.class);” locally i.e. declare in in4codes_selenium-webdriver each method it works but if I declare it in4codes_selenium-webdriver globally in class its giving me NULL pointer in4codes_selenium-webdriver exception.

public class CartPageSteps extends WebDriverManager { CartPage cartPage = PageFactory.initElements(driver, CartPage.class); @Test public void verifyValLogo() throws Exception { } 

Error Message :

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null 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.$Proxy12.isDisplayed(Unknown Source) at pageObjects.CartPage.verifyHomePage(CartPage.java:44) at Repaired_IETestCases.CartPageSteps.verifyHomePage(CartPageSteps.java:36) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132) at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822) at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) at java.base/java.lang.Thread.run(Thread.java:832) 

Total Answers 1
32

Answers 1 : of why getting NullPOinterException when page object is declared at instance level

I think object is not initialized that in4codes_browser-automation is the reason you are getting null in4codes_browser-automation pointer. Try below approach –

CartPage cartPage = null; @Before public void initTest(){ cartPage = PageFactory.initElements(driver, CartPage.class); } 

0