Quality Testing

Quality is delighting customers

java.lang.NullPointerException on clicking button in webdriver test

Hi All,

I am getting Null pointer exception on button click in web driver test.

(Code line is in bold)

below is the button imaage and html for application

And below is the written code with exception           

    String winHandleBefore = NavigateToCOSstart();
    int count;
    count = CartsCurrentCount();
    if (count != 0){
    driver.findElement(By.xpath("//button[@class='button btn-continue']")).click();

and exception is  :

    at Test.Maven.MyCartTest.MyCart(MyCartTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Views: 7192

Reply to This

Replies to This Discussion

It may be some Application Issue

driver.findElement(By.xpath("//button[@class='button btn-continue']")).click();

I think u r using custom xpath.

Try the below xpath(s), as the attribute type for a button is "input" not button:

driver.findElement(By.xpath("//input[@class='button btn-continue']")).click();


driver.findElement(By.xpath("//*[@class='button btn-continue']")).click();

The error indicates that findElement returned a null value and hence the click throws an error. You can install FirePath extension for FireBug and that will let you validate whether your xpath is giving any resulting nodes in the HTML or not

It seems either page loading is slow from selenium web driver', speed or web driver is not recognizing this xpath.
So please try to put wait for page loading or reconstruct the xpath.

You can try with this xpath


Seems to be that the object driver hasn't be inicializated.

instead of xpath use id

driver.findElement(By.id("//*[@class='button btn-continue']")).click();

still I am struggling with the same issue...

I have extended the base class where I wrote the methods as Start(), NavigateToCOSstart(),TestLogin() etc.

But when I remove these methods and put all straightforward lines of code, It works absolutely fine.

Not getting the reason why???

I think xpath is not an issue..otherwise it would have thrown the error as  element not found instead of null pointer exception....

it is immediately  after class starts , outside the method..

you must have handled the window pop up..just let me know , do you have method calls and action on application statments, on the same script ??

B'coz I modified my test, I haven't written a single line in test script .. All statements are written in method..It is working ....

but when I try to put any single line  like

driver.findElement(By(xpath("xyz")).click();   OR  driver.close();

 it gives me null pointer exception.

This is the problem of Chrome Browser and this is persistent error in Chrome Browser so I would suggest to use another browser like FF or IE for the same or use javascriptExecutor for the same.

Code for that is 

JavascriptExecutor jse = (JavascriptExecutor)driver; 

.executeScript("document.getElementByClassName('button btn-continue').click();")Hope this would help you in hitting the button.

NullPointerException is not from WebDriver side. It is java exception.

I think you might not initialized any variable properly. Just give a try with initializing all variables to some default value.


TTWT Magazine





© 2022   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service