1

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.

1
  • 3
    It's not an error, it's an Exception. Note the Exception tells you it occurs on line 25. Please edit your question to point out which line is line 25. Commented Jun 21, 2013 at 13:51

2 Answers 2

5

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply and guide to on this. now i can run the code without "NullPointerException"
great idea with the naming - I had similar issue and knew what you mean right away
1

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();
 }

}

1 Comment

Thank you for the reply. just i installed TestNG and ran the above testcase using TestNG. now i can run the testcase without "NullPointerException".

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.