Skip to content
Advertisement

The following error java.lang.NullPointerException is displayed after run the project

Problem: My classes are Hooks for setup and test. I try to click on cookie pop-up, using WebdriverWait, but don’t work. I have no idea why.

I am a beginner with selenium and automation testing and I am writing a selenium script using java, TestNG, and maven. When I write everything in one class, all works fine, but I want to have a package for all objects, a package for tests, and Hooks with the main setting. What I do public class Hooks { public WebDriver driver; @BeforeMethod public void Setup() throws InterruptedException { //set property for driver, Firefox instance System.setProperty(“webdriver.driver.firefox”, “C://SeleniumWebdrivers//chromedriver.exe”); //create driver object WebDriver driver = new ChromeDriver(); //Maximize page driver.manage().window().maximize(); //implicit wait driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.get(“http://google.com/”); //scroll // JavascriptExecutor js = (JavascriptExecutor) driver; // js.executeScript(“window.scrollBy(0,600)”); }

    @AfterMethod
    public void TearDown(){
       driver.quit();

    }

For test

public class Tests extends Hooks{
    @Test
    public void Test() throws InterruptedException{
        //Arrange
        HomePage homePage = new HomePage(driver);
        //Act
        WebDriverWait wait = new WebDriverWait(driver, 6000);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#onetrust-accept-btn-handler"))).click();
    
        //Assert
        Assert.assertEquals("EVALUAREA APARATULUI LOCOMOTOR", "EVALUAREA APARATULUI LOCOMOTOR")
    }

And the errors are:

java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:106)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:85)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at Tests.Test(Tests.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

How can I fix these errors and why do they occur?

What I tried: I tried to add Hooks class, I added different properties for my class

Advertisement

Answer

Basically the same answer as rzwitserloot gave but in a different order.

java.lang.NullPointerException

The stack trace complains about a NullPointerException. This means that a reference that should have pointed to an object was not pointing to anything (i.e. the reference was null).

So why was it null? For that you have to look at the next part of the stack trace. Each line describes a method. Each time a new method is called inside another a line is added. The format is <package>.<class>.<method>(<file:line>).

    at java.util.Objects.requireNonNull(Objects.java:203)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:106)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:85)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at Tests.Test(Tests.java:15)

So the first few lines aren’t your code. The first line is from java, the other ones are from selenium. Your code starts at Tests.Test(Tests.java:15).

So what happens at this line?

WebDriverWait wait = new WebDriverWait(driver, 6000);

At this line you are creating a new instance of WebDriverWait. You pass two arguments to the constructor. The second one is a number and clearly not null. The first one driver is a reference to a WebDriver. Because the other argument can’t be null, this one probebly is.

So why would driver be null?

You’ve declared the driver a field in the Hooks class. And by default if you don’t assign a value when declaring the field it’s value is null.

public class Hooks
{
    public WebDriver driver;

So problem located.

But you say, you are giving driver a value in the Setup method!

public class Hooks
{
    public WebDriver driver;
   @BeforeMethod
    public void Setup() throws InterruptedException
    {
        ...
        WebDriver driver = new ChromeDriver();
        ...
    }

Because you’ve added WebDriver before driver = new ChromeDriver(); it means you’ve created a local variable with the same name as the class field driver. Because local variables are more important then class fields the driver name in the Setup method now only refers to the local variable.

To assign the driver to the field you can either use this.driver = driver after creating the new ChromeDriver. Or you can remove WebDriver and only write driver = new ChromeDriver(); to assign the new ChromeDriver to the class field.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement