0

Code:public class PlaytechAutomationTest {

private WebDriver driver;

@Before
public void setUp() {
    // Setting system property for Chrome browser
     System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\WebDriver\\chromedriver.exe");
    // options.addArguments("--remote-allow-origins=*");
    //System.setProperty("webdriver.http.factory", "jdk-http-client");

    // Creating a ChromeDriver instance
    driver = new ChromeDriver();
}

@Test
public void testInternshipPosition() throws InterruptedException {
    // Navigating to Playtech website
    driver.get("https://www.playtech.ee");

Error: java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null

at PlaytechAutomationTest.testInternshipPosition(PlaytechAutomationTest.java:30)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

How to solve it?

1
  • Show us your imports Commented Mar 24, 2023 at 12:35

1 Answer 1

0

You have declared the driver object 2 times. First one is at the class level private WebDriver driver; and second one is at method level inside @Before method WebDriver driver = new ChromeDriver();.

The driver which is being used in @Test method is using the class variable driver, which is not initiated and hence throwing NPE.

Solution: Just declare the variable once. Change below line in @Before:

WebDriver driver = new ChromeDriver();

To:

driver = new ChromeDriver();
Sign up to request clarification or add additional context in comments.

4 Comments

Still having the same error
@AnupamKrishna, Can you change this line private WebDriver driver; to private static WebDriver driver;. Try and let me know if it works.
Now it is showing this-java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "PlaytechAutomationTest.driver" is null
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "PlaytechAutomationTest.driver" is null at PlaytechAutomationTest.testInternshipPosition(PlaytechAutomationTest.java:30) Note: 30 line- driver.get("playtech.ee");

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.