I'm getting null pointer exception for driver object.

Here is the flow:

  1. BaseClass initializes the driver (WebDriver driver = new ChromeDriver();)
  2. I create a test class and extend it with BaseClass. Now I am able to use all methods present in BaseClass example navigate, openBrowser, click , type, isLogin etc
  3. Then I create a utility class.
  4. There is if I am using navigate it is giving me null pointer exception though util class also extends BaseClass, because Util also uses methods present in BaseClass.

Flow is for Test Class is:

openBrowser("Chrome"); // driver present
method from BaseClass;driver present
method from BaseClass;driver present
method from BaseClass;driver present
method from util class;driver=null

What am I doing wrong?

Edit: I checked Thiago Fioravante's answer here WebDriver instantiation in Selenium framework but didn't quite get it, what he is saying. However, problem same i.e. I want to use same driver instance across the project.

Edit2: This navigate method is working in test Classes but not in Util Classes. It is giving driver = null

public void navigate(String url) {
        logger("Navigating to " + url);
  • Can you paste minimal representative code snippet? Of those your three classes. It it pretty hard to imaging the real picture from your description.
    – Alexey R.
    Commented Feb 21, 2018 at 11:25
  • @AlexeyR. I want to use same driver instance that I instantiated in BaseClass across the project. All my classes util classes, test classes have to extend BaseClass to use generic methods over there.
    – paul
    Commented Feb 21, 2018 at 11:29

2 Answers 2


With the little understanding I am replying to this question. As far as I understood UtilClass extends BaseClass and also Test Class extends BaseClass. But there is no where a UtilClass is extending your TestClass. Try creating an instance of the UtilClass in TestClass something like below in your code.

UtilClass uc = new UtilClass();
  • UtilClass is one, test classes are so many. How I will make it work for all test classes?
    – paul
    Commented Feb 21, 2018 at 11:31
  • I guess then you have to make a choice of extending either one of Base or Util in each Test Class depending upon the what method calls your going to make. Commented Feb 21, 2018 at 11:34
  • Do you mind checking the code via Team Viewer?
    – paul
    Commented Feb 21, 2018 at 11:35
  • I would love to do that after a while as I have limited internet access as of now. You can even convert your BaseClass into an interface. Where you can implement BaseClass and extend UtilClass in your TestClass at same time. Click here for reference. Commented Feb 21, 2018 at 11:37
  • Ok let me try, but give me a time slot so that we can connect at that time. It's 5.15pm here in Delhi, India.
    – paul
    Commented Feb 21, 2018 at 11:41

I have a different idea on creating the Utility methods. They should be common to all all classes in the architecture. So you can achieve this by making the methods inside the util class as public static. Then you can just call the method by class name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.