Mira
Mira

Reputation: 67

java.lang.NullPointerException webdriver

I'm a newbie in the autoamted testing using webdriver.Would somebody please check why I'm receiving

java.lang.NullPointerException
    at SuccessfullHoverTestCib.testSuccessfullHoverTestCib
    (SuccessfullHoverTestCib.java:29)
    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).....

I'm running the following code:

public class SuccessfullHoverTestCib {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        File pathToFirefoxBinary = new File("C:\\Documents and Settings\\chakarova\\Local Settings\\Application Data\\Mozilla Firefox\\firefox.exe");  
        FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
        WebDriver driver = new FirefoxDriver(firefoxbin,null); 
        baseUrl = "http://cibnew.sofia.ifao.net:7001/cib_web/web";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testSuccessfullHoverTestCib() throws Exception {
        driver.get(baseUrl);
        driver.findElement(By.id("inputButton")).click();
        driver.findElement(By.linkText("Hotels")).click();
        driver.findElement(By.id("pageLink_57")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

When I point to the error trace, it links to the Test method->driver.get(baseUrl); I think the problem is connected with the definition of the path to the firefox.exe,but I don't know how to fix it.Any help will be appreciated.

Upvotes: 3

Views: 9072

Answers (1)

JimEvans
JimEvans

Reputation: 27496

In your setUp() method, you are declaring a local WebDriver variable named driver, and setting that to a new driver instance. Your class-level driver variable is never set to an instance, and thus is always null.

public class SuccessfullHoverTestCib {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        File pathToFirefoxBinary = new File("C:\\Documents and Settings\\chakarova\\Local Settings\\Application Data\\Mozilla Firefox\\firefox.exe");  
        FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);

        // **** This is the line that changes.
        // **** Note the lack of the WebDriver type.
        driver = new FirefoxDriver(firefoxbin,null); 
        baseUrl = "http://cibnew.sofia.ifao.net:7001/cib_web/web";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    // The rest of your class should remain the same
}

Upvotes: 12

Related Questions