简体   繁体   中英

java.lang.NullPointerException when running a testcase via Selenium webDriver in Chrome

There's my class that uses Selenium WebDriver and throws the error.

public class Test_Chromedriver {
    public static WebDriver driver; 
    public static void main(String[] args) {
        try {
System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe");
            WebDriver driver=new ChromeDriver();
            driver.get("http://www.facebook.com");
            driver.findElement(By.id("email")).sendKeys("surya");
            driver.findElement(By.name("pass")).sendKeys("hello");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            driver.close();
            driver.quit();
        }
    }
}

once i ran the above code below Error Message is getting:

Started ChromeDriver
port=12877
version=23.0.1240.0
log=D:\Eclipse Juno Workspace\AS Selenium workspace\WebDriver Applications\chromedriver.log
Exception in thread "main" java.lang.NullPointerException
    at Test_Chromedriver.main(Test_Chromedriver.java:25)

Note: webdriver opening the chrome browser then entering the values into the fields, but above error is getting displayed all the times in console and finally block also not executing.

Your trouble come from your try/catch and specially your finally .

Because your driver is only localy in your try{} so... your finally can't do something with your driver. I'll rename your drivers to help you to see what happens here.

public class Test_Chromedriver {
    public static WebDriver driverNULL; // <- never instantiate
    public static void main(String[] args) {
        try {
            System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe");
            WebDriver driverOK=new ChromeDriver(); // < - local variable
            driverOK.get("http://www.facebook.com");
            driverOK.findElement(By.id("email")).sendKeys("surya");
            driverOK.findElement(By.name("pass")).sendKeys("hello");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            driverNULL.close(); // <- want to close a object never instantiate so null pointer exception
            driverNULL.quit();
        }
    }
}

What can you do now ?

public class Test_Chromedriver {
    public static WebDriver driver; 
    public static void main(String[] args) {
        try {
            System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe");
            driver=new ChromeDriver(); // <- remove the Webdriver type
            driver.get("http://www.facebook.com");
            driver.findElement(By.id("email")).sendKeys("surya");
            driver.findElement(By.name("pass")).sendKeys("hello");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            driver.close();
            driver.quit();
        }
    }
}

Maybe try to use TestNG and annotations?

http://testng.org/doc/index.html

They are making theirs work quite pretty good.

Using this type of structure will give you good head up for maintanence of tests and execution.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_Chromedriver {
    public static WebDriver driver; 


    @BeforeClass
    public void setUp(){
         System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe"");
         driver=new ChromeDriver();

}
   @Test 
    public static void TestFacebook() {
        try {
            driver.get("http://www.facebook.com");
            driver.findElement(By.id("email")).sendKeys("surya");
            driver.findElement(By.name("pass")).sendKeys("hello");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



 @AfterClass
 public void tearDown(){
     driver.close();
     driver.quit();
 }

}

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