0

I am new to Selenium and while running the below script I am getting null pointer exception and test stops running as soon as the site called loads. I am unable to understand the reason for exception.

The code is as given below:

package TestNGPackage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

public class ParameterizationTest {
WebDriver driver;
  @Test(dataProvider="getdata")
  public void Login(String Username, String Password) 
  {
    //WebDriverWait wait= new WebDriverWait(driver,60);
    //WebElement username= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
    WebElement username=driver.findElement(By.id("username"));
        WebElement password= driver.findElement(By.id("password"));
        username.sendKeys(Username);
        password.sendKeys(Password);
        WebElement SignIn= driver.findElement(By.tagName("button"));
        String text = SignIn.getText();
        if (text.equalsIgnoreCase("sign in")) {
            // sign in btn
            SignIn.submit();
        }
  }

  @DataProvider(name="getdata")
  public Object[][] testgetdata()
  {
      Object[][] data = new Object[3][2];
      //row1
      data[0][0]="[email protected]";
      data[0][1]="welcome23";
      //row2
      data[1][0]="[email protected]";
      data[1][1]="welcome23";
      //row3
      data[2][0]="[email protected]";
      data[2][1]="welcome23";

      return data;
  }

  @BeforeMethod
  public void beforeTest() {
      WebDriver driver = new FirefoxDriver();

      driver.get("http://52.25.202.63/illweb/");
    //  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

  @AfterMethod
  public void AfterTest() {
      //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        WebElement logout = driver.findElement(By.linkText("Logout"));
        logout.click();
        //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.close();

  }
}

I am getting the following error:

[TestNG] Running: C:\Users\Ronit\AppData\Local\Temp\testng-eclipse--473949223\testng-customsuite.xml [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite\Default test.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite exists: true FAILED CONFIGURATION: @AfterMethod AfterTest java.lang.NullPointerException at TestNGPackage.ParameterizationTest.AfterTest(ParameterizationTest.java:60) 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(MethodInvocationHelper.java:100) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:216) at org.testng.internal.Invoker.invokeMethod(Invoker.java:712) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129) 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:746) at org.testng.TestRunner.run(TestRunner.java:600) 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:1264) at org.testng.TestNG.runSuitesLocally(TestNG.java:1189) at org.testng.TestNG.runSuites(TestNG.java:1104) at org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) SKIPPED CONFIGURATION: @BeforeMethod beforeTest SKIPPED CONFIGURATION: @AfterMethod AfterTest SKIPPED CONFIGURATION: @BeforeMethod beforeTest SKIPPED CONFIGURATION: @AfterMethod AfterTest FAILED: Login("[email protected]", "p@ssw0rd") java.lang.NullPointerException at TestNGPackage.ParameterizationTest.Login(ParameterizationTest.java:20) 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(MethodInvocationHelper.java:100) at org.testng.internal.Invoker.invokeMethod(Invoker.java:646) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129) 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:746) at org.testng.TestRunner.run(TestRunner.java:600) 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:1264) at org.testng.TestNG.runSuitesLocally(TestNG.java:1189) at org.testng.TestNG.runSuites(TestNG.java:1104) at org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) SKIPPED: Login("[email protected]", "welcome123") SKIPPED: Login("[email protected]", "welcome123") =============================================== Default test Tests run: 3, Failures: 1, Skips: 2 Configuration Failures: 1, Skips: 4 =============================================== =============================================== Default suite Total tests run: 3, Failures: 1, Skips: 2 Configuration Failures: 1, Skips: 4 =============================================== [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\testng-failed.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite\testng-failed.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite exists: true [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 36 ms [TestNG] Time taken by org.testng.reporters.XMLReporter@5594a1b5: 30 ms [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\junitreports\TEST-TestNGPackage.ParameterizationTest.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\junitreports exists: true [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@3f3afe78: 15 ms [TestNG] Time taken by org.testng.reporters.jq.Main@1cd072a9: 228 ms [TestNG] Time taken by org.testng.reporters.EmailableReporter2@34ce8af7: 8 ms [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\toc.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\Default test.properties [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\index.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\main.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\groups.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\classes.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\reporter-output.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\methods-not-run.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\testng.xml.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\index.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old exists: true [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6b71769e: 58 ms

Please help me with what I am doing wrong, Thanks in advance!

1 Answer 1

6

You find a NullPointerException cause driver is not getting initialized before that point. You tried to initialize a WebDriver instance inside beforeTest() method, but it was local. To fix this, just initialize that WebDriver inside the beforeTest() method without redeclaring as:

driver = new FirefoxDriver();

not

WebDriver driver = new FirefoxDriver();

The second NullPointerExpception is caused due to the same reason.

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

1 Comment

Yes! Thank you. And well explained. This solved my problem.

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.