简体   繁体   中英

Why am I getting NullPointerException in my tests?

My test suite is organized into two parts: a library of test data, xpath locations for WebElements, .click() commands, etc. I also have a test suite.

This is an example test that shares the package it deals with for experimentation purposes. As of right this second I'm not worried about outputting results (I know how to do that), the code simply needs to work.

package LoginPage;

import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;

public class FunctionCheck {
      private WebDriver driver;
      private String baseUrl;

      @Before
      public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.XXXXXXXXXXX.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }
      @Test
      public void test() throws Exception {
        driver.get(baseUrl + "XXXXXXXXXXXX");
        Thread.sleep(600);
        LoginPage.enterValidCredentials myins = new LoginPage.enterValidCredentials();
        myins.run();
      }
      @After
      public void tearDown() throws Exception {
        driver.quit();
      }
}

This is the class "enterValidCredentials" that sits in the same package.

package LoginPage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class enterValidCredentials { // This class enters in a valid username and valid password on the login page.
    public void run() {
        WebDriver driver;
        driver.findElement(By.cssSelector("input[type=\"text\"]")).sendKeys("XXXXXX");
        driver.findElement(By.cssSelector("input[type=\"password\"]")).sendKeys("XXXXXX");    
    }
}

I got two problems:

  1. In the class "enterValidCredentials", it wants me to "initialize" the driver variable. The autofix for this in Eclipse sets line 8 to WebDriver driver = null; .
  2. Doing the above has my test throw a NullPointerException .

I've been searching through documentation for a while now. I'm really stumped as to what is going on. All I want is the run() method to literally run, and plug in the info to the page. That's the goal.

public class enterValidCredentials {
  public void run(WebDriver driver) {
    driver.findElement(By.cssSelector("input[type=\"text\"]")).sendKeys("XXXXXX");
    driver.findElement(By.cssSelector("input[type=\"password\"]")).sendKeys("XXXXXX");    
  }
}

Pass your existing WebDriver instance into enterValidCredentials.run :

myins.run(driver);

Here is your problem. driver is uninitialized. Because it is a local variable, the compiler will require you to initialize it. And when, as you said, you initialize it to null, the 2 following lines dereference it. You cannot call a method on a object that points to nothing.

            WebDriver driver;
            driver.findElement(By.cssSelector("input[type=\"text\"]")).sendKeys("XXXXXX");
            driver.findElement(By.cssSelector("input[type=\"password\"]")).sendKeys("XXXXXX");  

You must assign it a value

WebDriver driver = ...some object reference...;

Try to initialize your driver like

private WebDriver driver=new FirefoxDriver();

Mate, better use debug )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM