Do you have questions ? We got the answers!

Ask a question:

0
Getting NullPointer Exception running test using Selenium Webdriver

Solved 1 Answers 53 Views
I am running test using Selenium Webdriver in Java Language. My code is -:

public WebDriver driver;
    public String url="http://google.com/";
    @BeforeMethod
    public void setup() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C://Selenium driver//chromedriver.exe");
           WebDriver driver = new ChromeDriver();
           driver.get(url);
           driver.manage().window().maximize();
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
     @AfterMethod
     public void teardown() {
     System.out.println("Close");
     }
     @Test
     public void vertifyTitle(){
         String title = driver.getTitle();
         //System.out.println(title);
         Assert.assertEquals(title, "Google");
     }

I am getting the following error message

FAILED: vertifyTitle
java.lang.NullPointerException
    at commonTests.MainTests.vertifyTitle(MainTests.java:33)
    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:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:823)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:778)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)
    at org.testng.TestNG.runSuites(TestNG.java:1075)
    at org.testng.TestNG.run(TestNG.java:1047)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

1 Answer

0
Best answer

You are declaring and defining new driver instance at function level for the first function.

        WebDriver driver = new ChromeDriver();

It looks like you have another driver declared at class level. So you should use that. So your code should be as below.

         this.driver = new ChromeDriver();

Hope that helps!

answered Jan 16 by stbadmin (4,970 points)
selected Mar 3 by stbadmin

Top Contributors

  • 4970 pointsstbadmin

    34 questions 454 answers218 comments

  • 430 pointsSunilBhaskar

    40 questions 14 answers26 comments

  • 425 pointsMehedi

    36 questions 3 answers14 comments

  • 354 pointsSoft Tester

    43 questions 0 answers4 comments

  • 342 pointssumitkumarwatts

    0 questions 12 answers0 comments

  • 327 pointsKeeshaGolden

    0 questions 17 answers2 comments

  • 322 pointsVipul Sharma

    44 questions 0 answers24 comments

  • 310 pointsvysh.sharma

    25 questions 0 answers0 comments

  • 267 pointsOnkar_Nimje

    33 questions 15 answers15 comments

  • 258 pointsVanitha

    26 questions 0 answers6 comments

...