0

I have two separated Selenium Webdriver's classes. The class I made first runs without any error but the second class throws the NullPointerException.
The error points out the bellow line WebDriverWait wait = new WebDriverWait(driver,40);. not only for the WebDriverWait the NullPointer exception throws if driver is instantiated in a line of code. eg Actions action = new Actions(driver);.
The NullPointerException throws on the above code also. But the the first class doesn't have kind of issue.

The class I made first

package Initiate;

import dataProvider.CommonClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class NewBOM {

    public WebDriver driver;
    By newBomButton = By.id("btnUpdate");

    public NewBOM(WebDriver driver){
    this.driver=driver;
        this.CreateNewBOM();

    }

    public void CreateNewBOM(){
        Actions action = new Actions(driver);
        WebDriverWait wait = new WebDriverWait(driver, 40);
        wait.pollingEvery(2, TimeUnit.SECONDS);

        WebElement newBomBtn = driver.findElement(newBomButton);
        wait.until(ExpectedConditions.elementToBeClickable(newBomBtn));
        action.moveToElement(newBomBtn).click().build().perform();
    }
}

The second class which throws the nullpointer exception

package Initiate;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;


public class NewBOO {
    public WebDriver driver;

    public NewBOO(WebDriver driver){
        this.driver=driver;
    }

    public void test() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver,40);
        System.out.println("Test print");
    }
}

TestNg class for the class I made 1st

import Initiate.NewBOM;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class CreateBOM {
    public WebDriver driver;

    private NewBOM objNewBOM;

    @Test(priority = 1)
    public void clickOnNewBOMButton(){
        objNewBOM = new NewBOM(driver);
        objNewBOM.CreateNewBOM();
    }

}

TestNg class for the class I made 2nd

import Initiate.NewBOO;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class CreateBOO {
    public WebDriver driver;
    NewBOO objNewBOO;
    @Test(priority = 1)
    public void temporyTest() throws InterruptedException {
        objNewBOO = new NewBOO(driver);
        objNewBOO.test();
    }
}

Complete Error Messages

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at Initiate.NewBOO.test(NewBOO.java:17)
    at CreateBOO.temporyTest(CreateBOO.java:11)
    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:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    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:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)


===============================================
Default Suite`enter code here`
Total tests run: 1, Failures: 1, Skips: 0
===============================================


Process finished with exit code 0

I test both testNg classes individually and both classes are independent. But 1st class runs wothout any null pointer exception and 2nd class throws a nullpointer exception.

6
  • 3
    Where do you initialize the driver? Commented Oct 2, 2017 at 5:48
  • Which driver ? I initialized it on both classes Commented Oct 2, 2017 at 6:02
  • 1
    I don't see initialization anywhere in the code you provided. Commented Oct 2, 2017 at 6:05
  • 1
    Initializing must be something along the lines of public WebDriver driver = new WebDriver(), maybe with some additoinal options. The first class will run because it does not use the driver. Probably if you do, it will fail just the same. Commented Oct 2, 2017 at 6:51
  • 1
    Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Oct 2, 2017 at 6:55

2 Answers 2

1

It looks like your code didn't init WebDriver and try to use it, so it throws the NullPointerException. In TestNG, you can use @BeforeSuite, @BeforeTest, @BeforeClass to initialize your dependencies. The example below shows how to init the webdriver in 'BeforeClass'.

import Initiate.NewBOM;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class CreateBOM {
    public WebDriver driver;

    private NewBOM objNewBOM;

    @BeforeClass
    public void setupClass(){
        driver = new FirefoxDriver();
    }

    @Test(priority = 1)
    public void clickOnNewBOMButton(){
        objNewBOM = new NewBOM(driver);
        objNewBOM.CreateNewBOM();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Please Check your driver initiated, In case if driver is initiated two times so you may get the error where as second driver initiated . Eg. Actions act= new Actions(driver);

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.