0

I am stuck at a situation. Here i have created one testsuite with two test cases in it. First test case is executed properly. In second test case, it throws error of java.lang.NullPointerException at Webdriver driver

Test Case:

 public class second{
    private WebDriver driver;

    @Test
    public void sample() throws Exception
    {
           System.out.println("tab bar");
           driver.findElement(By.id("tabs"));
           //My code
        }
}
2
  • 1
    Are you injecting the instance of WebDriver ? Commented Jul 19, 2013 at 11:10
  • Could you please post the complete class Commented Jul 19, 2013 at 11:11

3 Answers 3

1
WebDriver driver;

is not initiated. What do you expect? The exception is absolutly correct. You can try the following code to avoid the exception:

driver= new WebDriver();
driver.findElement(By.id("tabs"));
Sign up to request clarification or add additional context in comments.

3 Comments

WebDriver is an interface and cannot be initialized, you should use the implementation like: WebDriver driver = new FirefoxDriver();
ok. I just wanne show him the basics. I dont know this interface.
Thanks for the reply...i had already initialized in one class
1
 private WebDriver driver; // initialize this

7 Comments

Thank for reply. But in every test case driver= new WebDriver(); is suppose to be initialized ? Am new to this so please consider
You may have to initialize in every class if you are going it with this way
In my first class also i have initialized "driver = new ChromeDriver"
Yes, that's why your first test case was success. You have to initialize driver here as well.
Else you may have to change your architecture using spring and use dependency injection
|
0

WebDriver is an interface and cannot be initialized, you should use the implementation like: WebDriver driver = new FirefoxDriver();

1 Comment

Yes i agree,but i initialized it in my "First Class", so do i need to do the same in "Second Class" ? Here "Second Class" is continued to the "First Class". E.g: In my "First Class" i did login and my "Second Class" contains script for further part. So what should i do in this case. I am new to this, so please advice.

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.