Skip to content
Advertisement

The following error java.lang.NullPointerException is displayed after run the project

Problem: My classes are Hooks for setup and test. I try to click on cookie pop-up, using WebdriverWait, but don’t work. I have no idea why.

I am a beginner with selenium and automation testing and I am writing a selenium script using java, TestNG, and maven. When I write everything in one class, all works fine, but I want to have a package for all objects, a package for tests, and Hooks with the main setting. What I do public class Hooks { public WebDriver driver; @BeforeMethod public void Setup() throws InterruptedException { //set property for driver, Firefox instance System.setProperty(“webdriver.driver.firefox”, “C://SeleniumWebdrivers//chromedriver.exe”); //create driver object WebDriver driver = new ChromeDriver(); //Maximize page driver.manage().window().maximize(); //implicit wait driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.get(“http://google.com/”); //scroll // JavascriptExecutor js = (JavascriptExecutor) driver; // js.executeScript(“window.scrollBy(0,600)”); }

JavaScript

For test

JavaScript

And the errors are:

JavaScript

How can I fix these errors and why do they occur?

What I tried: I tried to add Hooks class, I added different properties for my class

Advertisement

Answer

Basically the same answer as rzwitserloot gave but in a different order.

JavaScript

The stack trace complains about a NullPointerException. This means that a reference that should have pointed to an object was not pointing to anything (i.e. the reference was null).

So why was it null? For that you have to look at the next part of the stack trace. Each line describes a method. Each time a new method is called inside another a line is added. The format is <package>.<class>.<method>(<file:line>).

JavaScript

So the first few lines aren’t your code. The first line is from java, the other ones are from selenium. Your code starts at Tests.Test(Tests.java:15).

So what happens at this line?

JavaScript

At this line you are creating a new instance of WebDriverWait. You pass two arguments to the constructor. The second one is a number and clearly not null. The first one driver is a reference to a WebDriver. Because the other argument can’t be null, this one probebly is.

So why would driver be null?

You’ve declared the driver a field in the Hooks class. And by default if you don’t assign a value when declaring the field it’s value is null.

JavaScript

So problem located.

But you say, you are giving driver a value in the Setup method!

JavaScript

Because you’ve added WebDriver before driver = new ChromeDriver(); it means you’ve created a local variable with the same name as the class field driver. Because local variables are more important then class fields the driver name in the Setup method now only refers to the local variable.

To assign the driver to the field you can either use this.driver = driver after creating the new ChromeDriver. Or you can remove WebDriver and only write driver = new ChromeDriver(); to assign the new ChromeDriver to the class field.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement