1

This is my code:

 public class Init{
    public static WebDriver driver;
    @BeforeSuite
    public static void init_method1(){
        System.setProperty("webdriver.ie.driver", "//Provide IEDriverLocation//");
        driver = new InternetExplorerDriver();  
    }

}

public class B{
    @FindBy(xpath="XXXXxxxxxx")
    WebElement b_obj;

    public B(){
        PageFactory.initElements(Init.driver, this);
    }   

    public void b_method1(){
        b_obj.sendkeys();
    }

}

public class C{
    B b =  new B();
    @Test
    public void c_method1(){
        b.b_method1;
        }
}

In TestNG.xml,

<class name= "Package1.Init"></class>
<class name= "Package1.C"></class>

Error:

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 org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:302) at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:41) at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:288) at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:285) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238) at Package1.B.b_method1(B.java:8) at Package1.C.c_method1(C.java:3) 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:104) at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:756) at org.testng.TestRunner.run(TestRunner.java:610) at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) at org.testng.TestNG.runSuites(TestNG.java:1133) at org.testng.TestNG.run(TestNG.java:1104) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
newuser
  • 307
  • 3
  • 24
  • If I call B b = new B(); inside c_method1, it works. Could you anyone explain whats going on here. Thank you. – newuser Jan 18 '17 at 20:18
  • 1
    TestNg doesn't construct this class the way you might expect when using the test methods. So in some test frameworks, this would be fine. You'll have to follow the TestNg way of things though if you want to use that framework. – mrfreester Jan 18 '17 at 20:40
  • @mrfreester Thanks. Looks like variable declaration is compiled prior to any annotation in TestNG. Thus, B constructor gets the null driver to use to initiate elements using page factory. And thus throws Null pointer exception when trying to use webelements. – newuser Jan 18 '17 at 21:10
  • 1
    @jim-garrison for what it's worth, I don't think this is a duplicate of 'What is a null pointer' since that is asking what a null pointer is, and this question is asking about how TestNg initializes class properties differently then what you might expect. – mrfreester Jan 18 '17 at 21:34

0 Answers0