Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Are You Getting NullPointerException For Second TestNG Test While Running As A Suite?

Posted on 02/19/2025 By admin

Let’s look at some queries asked by people online below.

When I am running my TestNG class individually then it is passing but when I run multiple TestNG classes together second TestNG class is failing with NullPointerException.

The value assigned to a variable in @BeforeSuite method is not reflecting properly in @Test methods

I encounter Null pointer exception when using @BeforeTest, @BeforeMethod and trying to run two classes from testng.xml

Hopefully, you must have got some idea of the problem defined in the above links. There is a common problem in all links just the way of explaining may be different. Beginners and experienced Selenium professionals both generally face this issue and end up creating static variables most of the time as a solution. If you have not understood the problem, do not worry. I will make you understand step by step.

I have a common variable used in both @Test methods.

package SecondTestNullPointerException;

import org.testng.annotations.Test;

public class FirstTestNGClass {
        
        public String someVariable = "someValue";

        @Test
        public void firstMethodOfFirstTestNGClass()
        {
                System.out.println("Executing firstMethodOfFirstTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
        
        @Test
        public void secondMethodOfFirstTestNGClass()
        {
                System.out.println("Executing secondMethodOfFirstTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
}

Let’s run the above TestNG class.

Executing firstMethodOfFirstTestNGClass...
Value of someVariable is : someValue
Executing secondMethodOfFirstTestNGClass...
Value of someVariable is : someValue

Expected output. Just to be more precise, both @Test methods were able to access the value of the common variable “someVariable”.

Let’s create another TestNG class as below:-

Like the first TestNG class above, This class also has a common variable used in both @Test methods.

package SecondTestNullPointerException;

import org.testng.annotations.Test;

public class SecondTestNGClass {
        
        public String someVariable = "someValue";

        @Test
        public void firstMethodOfSecondTestNGClass()
        {
                System.out.println("Executing firstMethodOfSecondTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
        
        @Test
        public void secondMethodOfSecondTestNGClass()
        {
                System.out.println("Executing secondMethodOfSecondTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
}
Executing firstMethodOfSecondTestNGClass...
Value of someVariable is : someValue
Executing secondMethodOfSecondTestNGClass...
Value of someVariable is : someValue

Both First and Second TestNG classes have a similarity that they share a common variable “someVariable”. Let’s understand it in real-time practice with respect to selenium WebDriver scripts.

We require a browser to be launched for Selenium Scripts and launched browser to be closed at end of scripts. We may require some set up like establishing a database connection, an excel reader instance, any common variable, etc. One way is to have these stuff locally for each Test or class or use TestNG annotations to set it up once at suite or test or class level.

Let’s put the common variable “someVariable” under a TestNG annotation @BeforeSuite as we need the above variable to be initialized once and extend Setup class to both TestNG classes as below.

package SecondTestNullPointerException;

import org.testng.annotations.BeforeSuite;

public class Setup {

        public String someVariable;

        @BeforeSuite
        public void setupVariable() {
                System.out.println("Executing setupVariable...");
                someVariable = "someValue";
        }

}
package SecondTestNullPointerException;

import org.testng.annotations.Test;

public class FirstTestNGClass extends Setup{
        
        @Test
        public void firstMethodOfFirstTestNGClass()
        {
                System.out.println("Executing firstMethodOfFirstTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
        
        @Test
        public void secondMethodOfFirstTestNGClass()
        {
                System.out.println("Executing secondMethodOfFirstTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
}
package SecondTestNullPointerException;

import org.testng.annotations.Test;

public class SecondTestNGClass extends Setup{
        
        @Test
        public void firstMethodOfSecondTestNGClass()
        {
                System.out.println("Executing firstMethodOfSecondTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
        
        @Test
        public void secondMethodOfSecondTestNGClass()
        {
                System.out.println("Executing secondMethodOfSecondTestNGClass...");
                System.out.println("Value of someVariable is : "+ someVariable);
        }
}

Let’s run both TestNG classes individually.

Executing setupVariable...
Executing firstMethodOfFirstTestNGClass...
Value of someVariable is : someValue
Executing secondMethodOfFirstTestNGClass...
Value of someVariable is : someValue
Executing setupVariable...
Executing firstMethodOfSecondTestNGClass...
Value of someVariable is : someValue
Executing secondMethodOfSecondTestNGClass...
Value of someVariable is : someValue

We can see how @BeforeSuite annotated method was called to initialize variable. Now let’s run both TestNG classes together using a TestNG xml.



        

Did you observe that for the second TestNG class you get the value of common variable “someVariable” as NULL? If you perform any action on this variable you will encounter NullPointerException.

As we know @BeforeSuite will be executed once and we are initializing common variables there. Each TestNG class extends class “Setup” which has @BeforeSuite method. But still, we are getting NULL value for the second test but not for the first test.

To understand the reason behind this behavior, you need to understand how does Inheritance works and TestNG annotations are grouped for execution.

We know that the class inherits the properties of another class using Inheritance. So in the above TestNG classes, “someVariable” is being inherited from Setup class and both TestNG classes have an individual instance of “someVariable”. TestNG groups annotations from all TestNG classes and run as per the mentioned annotation.

So in the above setup both TestNG classes have their own instance of Setup class. TestNG will club @BeforeSuite methods from both instances of Setup class as only one while running as a suite. So when @BeforeSuite method runs it initializes the common variable “someVariable” for first TestNG class which was consumed by @Test methods of First TestNG class. When the execution of the Second TestNG class begins, @BeforeSuite will not be executed again which results in the non-initialization of very own copy of variable “someVariable” in SecondTestNGClass and it remains NULL.

Let’s try to understand the above concept using a diagram. Below is the initial setup we did. Class Setup is extended by both TestNG classes.

While running as a suite:-

You can see both TestNG class have their own instance of a class variable “someVariable” but the method which initializes that variable executed only once as per TestNG. There will not be any execution of @BeforeSuite method for the SecondTestNGClass separately and the inherited instance variable “someVariable” will not be initialized for SecondTestNGClass.

If you have multiple TestNG classes running together then only the first TestNG class will run fine and all other TestNG classes will fail with NullPointerException. I hope I am able to explain the actual problem and how generally we unwantedly do this and face NullPointerException.

We will see some solutions to this problem in other posts but the common solution to this problem is to change instance variables to class variables or static variables so that if it is initialized once it will be used by others (That is how a static variable works).

package SecondTestNullPointerException;

import org.testng.annotations.BeforeSuite;

public class Setup {

        public static String someVariable;

        @BeforeSuite
        public void setupVariable() {
                System.out.println("Executing setupVariable...");
                someVariable = "someValue";
        }

}
Executing setupVariable...
Executing firstMethodOfFirstTestNGClass...
Value of someVariable is : someValue
Executing secondMethodOfFirstTestNGClass...
Value of someVariable is : someValue
Executing firstMethodOfSecondTestNGClass...
Value of someVariable is : someValue
Executing secondMethodOfSecondTestNGClass...
Value of someVariable is : someValue

If you change instance variables to static variables, you should use it with ThreadLocal so that it will not create any problem while running in parallel.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 74 – Problem With JSONassert While Ignoring Fields From JSON Documents
Next Post: Git Tutorial 20 – Untracked File Vs Unstaged File

Related Posts

December 17, 2017 – Make Selenium Easy Uncategorized
Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern Uncategorized
Selenium Framework 3: Types Of Selenium Frameworks Uncategorized
ParamsInPostman – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
@JsonInclude in jackson Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark