Nilesh Khoriya Nilesh Khoriya - 26 days ago 8
Java Question

Selenium TestNG framework unable to call driver object in multiple test

I am trying to execute the following program but getting a null pointer error. where @test is not able to access the object - driver. what could be the possible error i am making. Error -

JavaScript warning:,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,d,csi/am=wCJGjhccAPk_IRQStxAWyAImDiA/rt=j/d=1/t=zcms/rs=ACT90oGwMPBWhsFBKoM1svJAFUVoVQRQug, line 7: mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
FAILED: websiteTitle

at myPackage.TestNGforHDFC.websiteTitle(
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(

package myPackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGforHDFC {
public WebDriver driver;
public String urlUnderTest = "";
public String projLocation = "C:\\Users\\Nikita Agrawal\\Selenium\\geckodriver.exe";
public void login()
System.setProperty("webdriver.gecko.driver", projLocation);
WebDriver driver = new FirefoxDriver();

public void websiteTitle()

Answer Source

The class variable driver is never initialized, you're defining a new variable called driver inside the login method.


public void login(){
    driver = new FirefoxDriver();

Instead of:

public void login(){
    WebDriver driver = new FirefoxDriver();