Skip to content
Advertisement

i am getting a java.lang.NullPointerException while executing the testng script

while running the code i am getting this java.lang.NullPointerException in chrome, i have extended the main login class to another and its showing this exception. i have extended the same main class to another diff class and its successfully running but only this class is showing error.

here is the code

//login page public class admin_login {

JavaScript

}

//Userclass

public class User extends admin_login{

JavaScript

i have tried to get normally and then via try catch also but its still showing the same exception and test failed

stack trace

JavaScript

Advertisement

Answer

  • 1 java.lang.NullPointerException appeared because the driver was null.

    You’ve decided to initialize the driver in login() test-method. So user() will work only when login() method has been executed before (and the driver was initialized).

  • 2 When you run some Test class that is extended from another Test class, tests will be invoked for both classes. But the order might be different. At least I’ve found different behavior when the class name started from A and the class name started from Z. So that might be the reason why it works for some other class (with another name).

Solution

I suggesting:

Initialize common test resources in @Before configuration methods.

e.g.

JavaScript

So you’ll avoid the test-methods execution order dependencies.

Advertisement