Skip to content
Advertisement

Null pointer exception while running selenium test. I am using Page object model

Below is my Testbase class:

JavaScript

My VerifyLaydownlogintest as below:

JavaScript

And this is my Login page class:

JavaScript

This is error of NPE:

JavaScript

I tried to resolve this but unable to find the root cause. Where am I going wrong?

My config.properties

JavaScript

Advertisement

Answer

You are using TestNG as your test automation framework.

In TestNG, execution will take place according to the annotation that you define in your test class.

@BeforeMethod has higher priority than @Test or @AfterMethod

and if you take a look at your @BeforeMethod

JavaScript

the first method that you are calling is initialization(); and the first line in that method is String browserName=prop.getProperty("browser");

You are calling prop here from public static Properties prop; and it is a static variable and you’ve not initialized it there, hence you are getting the NPE. Since prop is just a variable that you are trying to access.

You should rather create an object of TestBase class in your @BeforeMethod

Something like this:

JavaScript

Now once you create an instance of TestBase like above, the below constructor will get invoked

JavaScript

And since prop is a static variable, the assigned value to this variable will persist.

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