Selenium – null pointer exception_using java selenium webdriver with TestNG

seleniumselenium-webdrivertestng

when execute the code below , null pointer exception is occures, as driver of class Pom_MainHerokuapp is always null

testcases:-

package testcases;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import poms.Pom_MainHerokuapp;
import testbase.TestBase;

public class MainHerokuapp extends TestBase {
    Pom_MainHerokuapp mainHerokuappObject;
    public MainHerokuapp() {
        mainHerokuappObject = new Pom_MainHerokuapp(driver);
    }

    @Test(priority = 0)
    public void TestMainpagetitle() {
        mainHerokuappObject.VerifyTitles();
    }

    @Test(priority = 1)
    public void TestABTestingText() {
        mainHerokuappObject.VerifyTextOfABTesting();
    }
}

TestBase class:-

package testbase;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class TestBase {
    public WebDriver driver;

    @BeforeTest
    public void setup() {
        driver = new FirefoxDriver();
        driver.get("https://the-internet.herokuapp.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
}

Also:

package poms;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;

public class Pom_MainHerokuapp {
    public WebDriver driver;
    String text;
    String StringMaintext;
    String Stringsubtitle;

    @FindBy(xpath = "//html//body//div[2]//div//h1")
    WebElement Maintitle;

    @FindBy(xpath = "//html//body//div[2]//div//h2")
    WebElement Subtitle;

    @FindBy(linkText = "A/B Testing")
    WebElement ABTesting;

    @FindBy(xpath = "//html//body//div[2]//div//div//h3")
    WebElement ABTestingText;

    create constructor of this class
    public Pom_MainHerokuapp(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this); // Initialization all webelements  
    }

    public void VerifyTitles() {
        StringMaintext = Maintitle.getText();
        Stringsubtitle = Subtitle.getText();
        System.out.println(StringMaintext);
        System.out.println(Stringsubtitle);
        Assert.assertEquals(StringMaintext, "Welcome to the Internet");
        Assert.assertEquals(Stringsubtitle, "Available Examples");
    }

    public void VerifyTextOfABTesting() {
        ABTesting.click();
        text = ABTestingText.getText();
        System.out.println(text);
        Assert.assertEquals(text, "A/B Test Variation 1");
    }
}

the error is :-

FAILED: TestMainpagetitle 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.$Proxy7.getText(Unknown Source) at
poms.Pom_MainHerokuapp.VerifyTitles(Pom_MainHerokuapp.java:36) at
testcases.MainHerokuapp.TestMainpagetitle(MainHerokuapp.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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) at
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774) at
org.testng.TestRunner.run(TestRunner.java:624) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at
org.testng.SuiteRunner.run(SuiteRunner.java:261) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at
org.testng.TestNG.run(TestNG.java:1048) at
org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112) at
org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at
org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

FAILED: TestABTestingText 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.$Proxy7.click(Unknown Source) at
poms.Pom_MainHerokuapp.VerifyTextOfABTesting(Pom_MainHerokuapp.java:46)
at testcases.MainHerokuapp.TestABTestingText(MainHerokuapp.java:33)
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:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) at
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774) at
org.testng.TestRunner.run(TestRunner.java:624) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at
org.testng.SuiteRunner.run(SuiteRunner.java:261) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at
org.testng.TestNG.run(TestNG.java:1048) at
org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112) at
org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at
org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

Best Answer

This isn't hard to explain. The default constructor of your test class MainHerokuapp will be called as soon as it is run, i.e. when driver is still null - before your @BeforeTest method, where driver gets set.

MainHerokuapp and TestBase seem to be mixed-up rather than having a clear separation, so you'd be better off merging them back into one.

Another way is to restore control to the child class by dropping the constructor, moving the @BeforeTest there, and calling up to the parent. This definitely works:

public class MainHerokuapp extends TestBase {
    Pom_MainHerokuapp mainHerokuappObject;

    @BeforeTest
    public void setup() {
        super.setup();
        mainHerokuappObject = new Pom_MainHerokuapp(driver);
    }

    @Test(priority = 0)
    public void TestMainpagetitle() {
        mainHerokuappObject.VerifyTitles();
    }

    @Test(priority = 1)
    public void TestABTestingText() {
        mainHerokuappObject.VerifyTextOfABTesting();
    }
}

public class TestBase {
    public WebDriver driver;

    // @BeforeTest
    public void setup() {
        driver = new FirefoxDriver();
        driver.get("https://the-internet.herokuapp.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
}
Related Topic